views:

111

answers:

2
$messages = $db->query("SELECT * FROM chatmessages ORDER BY datetime DESC, displayorderid DESC LIMIT 0,10");
while($message = $db->fetch_array($messages)) {
    $oldmessages[] = $message['message'];
}
$oldmessages = array_reverse($oldmessages);
?>
<div id="chat">
<?php
for ($count = 0; $count < 9; $count++) {
    echo $oldmessages[$count];
}
?>
<script language="javascript" type="text/javascript">
<!--
setInterval( "document.getElementById('chat').innerHTML='<NEW CONTENT OF #CHAT>'", 1000 );
-->
</script>
</div>

I'm trying to create a PHP chatroom script but I'm having a lot of trouble getting it to AutoRefresh

The content should automatically update to , how do you make it do that? I've been searching for almost an hour

+1  A: 

I would take that PHP functionality you have and putting it in a sperate page that returns JSON. From there you can call that method using jQuery and the AJAX tools built in. Really simple. Start here for jQuery: http://api.jquery.com/category/ajax/

Dustin Laine
As much as I love JSON to work with, I've found a lot of hosts and servers don't have it enabled on PHP by default (Espec as the JSON_FORCE_OBJECT constant was not added until 5.3, and not everyone has upgraded yet), and don't like enabling it, so I think that XML would probably be a better way to go, until it becomes more widely supported.
Psytronic
Then use SOAP over JSON, either way you will want to use some sort of async method to retrieve that data.
Dustin Laine
A: 

you'll need to set up a server side script that renders only the contents of the chat div and use ajax to grab that. it can be done with jquery quite easily:

In your html document:

<head>
...
    <script src="/path/to/jquery.js" type="text/javascript"></script>
    <script>
        var chatUpdateInterval = null;
        function initChat() {
            chatUpdateInterval = window.setInterval(updateChat, 5000); /* every 5 seconds */
        }
        function updateChat() {
            $.ajax({
                url: '/url/path/to/your/script.php'
               ,dataType: 'HTML'
               ,success: function(data, status, xhr){
                   $('#chat').append($(data).html());
               }
            });
        }
        $(document).ready(function(){
             initChat();
        });
    </script>
...
</head>
<body>
    <div id="chat">
      please stand by while we're firing up the coal!
    </div>
</body>

Note that this won't be really good, it's just a sample to get you started. you should look into jquery's $.ajax

Kris