I have a chat program which uses ajax to run a php script to query a MYSQL DB, pulls the chat details and provides it to javascript which then pushes it to a div.
At present, the data appears at the top of the div and works it's way down, and the goal is to have the latest comment to appear from the bottom working its way up the screen.
Current code snippets:
AJAX:
function displayChat() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("chat").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","displayChat.php?",true);
xmlhttp.send();
}
HTML Chat DIV
<div id="chat"></div>
PHP code
$displayChat =mysql_query(
"select c.userID, u.userName, c.comments, c.datetime, c.tag
from chat c
inner join userAccounts u
on u.userID=c.userID
order by c.datetime ASC",
$connection);
while ($row = mysql_fetch_assoc($displayChat )) {
echo "(".$row['datetime'].")"." <b>".$row['userName']."</b>: ".$row['comments']."<br>";
}
CSS
#chat {
height: 90%;
overflow-y: scroll;
background-color: white;
text-align: left;
padding-left: 25px;
bottom: 0;
}
How can I achieve this?