I am implementing chat using PHP, SQL, JavaScript. I am checking for new messages by reloading the same file using window.location.replace(‘chatlog.php’)
So the page is redirected to itself after every interval. When there are more number of messages, I am getting a scroll bar at the side, and as the page is reloading I had to scroll it to see the last posted message every time is refreshes which is hard to do.
How do I write the code so that the last posted message is displayed even the page reloads?
Some part of the chat.php file is as below
while($record=$mysql_fetch_array($sql))
{
print "<font color="brown">$record[firstname]:</font><font color="black">$record[message]</font>";
}
<script>
setTimeout("window.location.replace('chatlog.php')",2000);
</script>
Can AJAX be used to solve this? If so, how do we do that ?
I have another question. I am storing timestamp in sql table as year-month(in numbers)-date hours:min:sec
I want to display it in month date at time am/pm for example (April 23rd 2012 at 5pm)
How do I convert to this? Are there any php functions to convert?
Please help !!!!!
The simplest solution is to just put an
<a name="msgID_$id">...</a>name anchor on each message, eg:then embed a refresh that has an anchor corresponding to the LAST msg ID you listed on the page:
when the page refreshes, the browser will auto-scroll to that message, and the new refresh’s anchor will have the ID of the newest message instead (e.g. #msgID_25).