Question:

AJAX: Auto-Refresh with PHP?

by  |  earlier

0 LIKES UnLike

I'm having trouble getting a portion of the site I'm developing to auto refresh every 10 seconds using AJAX. Here's what I'm doing that isn't working quite right. Your guidance is very much appreciated!

<!-- Creates the XMLHttpRequest Object for AJAX Functionality -->

function createXHR(){

var request = false;

try {

request = new ActiveXObject ('Msxml2.XMLHTTP');

}

catch (err2) {

try {

request = new ActiveXObject ('Microsoft.XMLHTTP');

}

catch (err3) {

try {

request = new XMLHttpRequest();

}

catch (err1) {

request = false;

}

}

}

return request;

}

<!-- AJAX Content Refresh Every 10 Seconds -->

function refreshContent(){

var xhr = createXHR();

if (xhr==null){

alert("It appears that your browser is a dinosaur...update it and come back.");

return;

}

var url = "refreshContent.php";

var params = "sid="+Math.random();

xhr.open ("POST", url, true);

xhr.setRequestHeader ("Content-type", "application/ x-www-form-urlencoded");

xhr.setRequestHeader ("Content-length", params.length);

xhr.setRequestHeader ("Connection", "close");

xhr.onreadystatechange = function(){

if(xhr.readyState == 4 && xhr.status == 200){

document. getElementById ("news").innerHTML = xhr.responseText;

}

}

xhr.send (params);

setTimeout ("refreshContent()", 1000);

}

Then I've got this for the HTML onload event:

<body onload= "refreshContent()">

-------------------------

Like I said, for some reason it's not updating the contents of the page without a page refresh...HELP!

 Tags:

   Report

3 ANSWERS


  1. Try sticking an alert() in refreshContent to debug when and if the function is even going off.  Add one also to the readystatechange function printing the responseText.

    Sorry I can&#039;t be much more help that at the moment.


  2. Looks good to me, have you checked the error console?

    Also you should probably note that setTimeout (&quot;refreshContent()&quot;, 1000) refreshes every second, not every 10 seconds. Could this be the problem, trying to get the request too quickly and creating a loop? Your script will only post the information if the loading is completed.

    Edit: I&#039;ll stay on this post, so you can add details and I&#039;ll respond..

    By the way, nice best answer percentage. 53% is way more than me, and you&#039;ve answered less questions.

    Edit2: Are you sure there&#039;s something in refreshContent.php? Maybe you&#039;re getting an error that isn&#039;t showing up when you try to get the responseText.

  3. var url = &quot;refreshContent.php&quot; is the problem making you can&#039;t refresh.

    If you website is http://url.com. You must make another site that is http://url.com/refreshContent.php. That site you made ,must have php the refresh code.

    If still can&#039;t you do this,

    &lt;html&gt;

    &lt;head&gt;

    &lt;script type=&quot;text/javascript&quot;&gt;

    &lt;!--

    function delayer(){

        window.location = &quot;http://yoursite.com&quot;

    }

    //--&gt;

    &lt;/script&gt;

    &lt;/head&gt;

    &lt;body onLoad=&quot;setTimeout(&#039;delayer()&#039;, 50000)&quot;&gt;

    And this will refresh the site every 50 secs

    1000=1secs

    This is not a refresh script but is more likely to be a redirect script.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.