View Full Version : Real Time Chat

03-24-2012, 05:06 PM
I've just started using AJAX to receive data from a database and quite a bit of the forums I've seen explain stuff that seem to complicated for me at the moment.

url: "includes/refresh.php",
dataType: 'html',
success: function(data) {
if (message_data != document.getElementById("messages").innerHTML) {
if(document.body.className == 'blurred') {
document.title = "[New Message]";
setTimeout("document.title = \"Real Time Chat\"", 2000);
if(document.getElementById("loading") == "Loading... Please Wait") {

The above code works fine, however, how would I approach getting this to work with JSON (As I've been told that's a lot better than retrieving a bunch of messages through HTML)

Heres the PHP code of refresh.php:


$sql = mysql_query("SELECT * FROM realchat ORDER BY id DESC LIMIT 0,30");
while($result = mysql_fetch_array($sql)) {
if($result['id'] & 1) {
echo "<div style='background-color:rgba(180,180,180,0.90);' class='message'>";
else { echo "<div class='message'>"; }
if ($result['author'] == "") {
$author = "Anon";
} else {
$author = $result['author'];

echo stripslashes("[".$author."]: ".$result['message']);
echo "</div>";

All help will be appreciated and if anyone could explain each new line they add/ change, that would be much more helpful, thanks!

05-01-2012, 12:59 PM
I am not sure to what level you need the explanation.
But if you want my advice, choose one of the chat services out there.
They made tons of adjustments, they support ssl, they have social integration
so...you can try one of the many 123flashchat, rumbletalk, cbox....