Forum Moderators: coopster

Message Too Old, No Replies

Need to auto refresh a PHP page using ajax

Ajax auto refresh php

         

ganesanji

8:05 am on Dec 2, 2008 (gmt 0)

10+ Year Member



Hi all,

I am developing a instant messneger chat application.

I need to refresh a page for every 5 sec using ajax without using DIV tags.

I need to refresh the page every 5 seconds to check the DB for waiting messages, if the waiting messages exist in DB I need to popup a chat window.

If it is possible please help me....It will be appreciated.

Please give me some suggestions how to do that....

thanks in advance...

regards,
Ganesan

vincevincevince

10:43 am on Dec 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



  • Don't refresh; use setTimeout() and use it to call a function which makes an AJAX request
  • Do real AJAX with XML data - then read the values from the data you've sent and act appropriately by firing popup events or similar
  • Scale the polling frequency according to message frequency: if you've just had or sent a message, check again every two seconds for a minute or so; if no messages were sent or received in the last five minutes, only check every 15 seconds.
  • ganesanji

    11:12 am on Dec 2, 2008 (gmt 0)

    10+ Year Member



    thanks for ur reply vincevince. I already used the setTimeout function with ajax..But the problem is it is not prompting the popup window, while it gets the xmlHttpResponse from the php page.

    I have given below the javascript/ajax and php i used.

    function PMstateChanged(){

    if (xmlHttp.readyState==4)
    {


    document.getElementById("openPMbox").innerHTML=xmlHttp.responseText;

    xmlHttp = null;
    }
    }

    function openPMBox(ID) {

    setInterval("PMBoxOpen('"+ID+"')", 5000 );

    }
    function PMBoxOpen(uID) {

    xmlHttp = GetXmlHttpObject();
    var url = "openPMbox.php?uID="+uID;

    xmlHttp.onreadystatechange=PMstateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);

    }

    Code of openPMbox.php is...

    <?
    ob_start();
    session_start();
    $uid=$_SESSION['login_user_id'];
    $con=mysql_connect("localhost","root","");
    mysql_select_db("sweet60");
    $sql1 = "SELECT * FROM `chat_messages` WHERE touserID='".$_SESSION['userID']."' AND status='0'";
    $query1 = mysql_query($sql1)or die(mysql_error());
    while ($res1 = mysql_fetch_array($query1)) {

    //echo "you are having pending message";
    //echo $res1['sessionID'];
    echo "<script language='javascript'>openChat(\"Messenger/convo.php?sessionID=".$res1['sessionID']."\", \"Convo\");</script>";
    }
    ?>

    i called the openPMBox() function in onload event. All the functions are working but it is not promt the popup window. I am strucking here ..please help me...it would be great help for me

    thanks in advance....

    vincevincevince

    11:16 am on Dec 2, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You need to do some debugging I'm afraid (and I can't do that from what you've sent). Try testing in Firefox with the Firebug extension - that's a great way to find out what happens - and allows you to see the stack & variable values as they change.

    First I'd change openChat(...) into alert('I got a message') and be sure it gets that far; if it does, I'd change it back and track from function openChat() onwards (which you didn't paste here).