0

I have a php page that needs to be refreshed every 5 secs. On embedding the ajax file, I don't find the updates taking place in Firebug. Here is the skeleton of the code:

**notification.php**
<?php
       ....
       ....
?>
<html>
     <head>
     <script src="./refresh.js"></script>
     <script type="text/javascript">
         refreshContents();
     </script>
     </head>
     <body>
            ....

            <div id="identifier">
               <p>Waiting area</p>
            </div>
      </body>
</html>


**refresh.js**

var seconds = 5;
var content = "identifier";
var url = "notification.php";

function refreshContents()
{
   var xmlHttp;
   try
   {
      xmlHttp = new XMLHttpRequest();
   }
   catch(e)
   {
      try
      {
         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(f)
      {
         try
         {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch(g)
         {
            alert("Browser not supports Ajax");
            return false;
         }
      }
   }


   xmlHttp.onreadystatechange=function()
   {
      if (xmlHttp.readyState == 4)
      {
         document.getElementById(content).innerHTML = xmlHttp.responseText;
         setTimeout('refreshContents()', seconds*1000);
      }
   }

   xmlHttp.open("GET", url, true);
   xmlHttp.send(null);
}

var seconds = 5;
window.onload = function startrefresh(){
   setTimeout('refreshContents()', seconds*1000);
}
name_masked
  • 9,544
  • 41
  • 118
  • 172

3 Answers3

9

Though it may not be the ideal solution, jQuery has a pretty simple way of implementing exactly this:

$(document).ready(function () {
  function reload() {
    $("#content").load("notification.php");
  }
  setTimeOut(reload, seconds*1000)
}

I'm not sure that will work perfectly, haven't done it in a little while, but its a much more elegant solution I do believe.

jrbalsano
  • 834
  • 7
  • 18
3

Why not just put a <meta http-equiv="refresh" content="5"> tag into your <head>? It will refresh the page every 5 seconds without the need for any javascript.

Ryan
  • 925
  • 2
  • 6
  • 25
  • Because I need to fire updates as well every 5secs to the database. – name_masked Jun 02 '11 at 19:59
  • Fire them in your PHP code, since it will run every time the page is refreshed – Ryan Jun 02 '11 at 20:00
  • Doesn't seem to be working. This is really strange !! is it because notification.php creates a cookie in the beginning? – name_masked Jun 02 '11 at 20:12
  • What kind of actions does notification.php do? If you're refreshing the entire page and not just replacing some of it's contents, it makes more sense to use the `meta http-equiv="refresh"`. What kind of results do you get when you open this page? – jrbalsano Jun 02 '11 at 20:28
1

see the following example :

<html>
<head>
<title>Refresh a page in jQuery</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>
<body>
<button id="PageRefresh">Refresh a Page in jQuery</button>
<script type="text/javascript">
$('#PageRefresh').click(function() {
          location.reload();
});
</script>
</body>
</html>

this code definetly help you. or we can use -

 <meta http-equiv="refresh" content="5">
Gaurav Gupta
  • 478
  • 6
  • 10