Forum Moderators: open

Message Too Old, No Replies

AJAX and PHP Auto Refresh

Auto Refresh a PHP includen using AJAX

         

LooInSpain

3:34 pm on Feb 2, 2008 (gmt 0)

10+ Year Member



Hi,

I've never used AJAX before and I'm having some real problems trying to make it work with IE7.

What I have is a script to auto refresh a php included page. The php page has an order by rand() mysql query so that it will return different results each time the page is refreshed. What I want is this page to auto refresh using AJAX and change the top 5 results from the query.

The script works perfectly in Firefox, but in IE7 it keeps throwing up an error.

Has anyone else had this problem, and if so how did you fix it.

Here is the code I'm currently trying to work with:


<SCRIPT language=JavaScript type="text/javascript">

GetXmlHttpObject = function()
{
var XMLHttp = null;
// active x internetexplorer ---------------------------------------------------------
try
{
XMLHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0'); // ie7
}
catch (e)
{
try
{
XMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //ie 6
}
catch(e)
{
try
{
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");//ie older versions
}
catch(e)
{
}
}
}
// active x internetexplorer ---------------------------------------------------------

if (XMLHttp == null)
{
XMLHttp = new XMLHttpRequest();
}
return XMLHttp;
}

// Make the XMLHttpRequest object
var http = GetXmlHttpObject();

function sendRequest(page) {

// Open PHP script for requests
http.open('get', page);
http.onreadystatechange = handleResponse;
http.send(null);

}

function handleResponse() {

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

// Text returned FROM the PHP script
var response = http.responseText;

if(response) {
// UPDATE ajaxTest content
document.getElementById("msgstatus").innerHTML = response;
}

}

}

function repeatloop()
{
sendRequest('featured.php'); // replace "inbox-status.php" with your php page's url
setTimeout("repeatloop()", 20000);
}

window.onload=function() {
repeatloop();
}
</script>

If anyone can help me try to fix this problem it would be great, all help appreciated.

Thanks

eelixduppy

9:22 pm on Feb 5, 2008 (gmt 0)



Hello and Welcome to WebmasterWorld!

Can you please include the error you are receiving to make debugging a little easier? Thanks.

LooInSpain

11:04 pm on Feb 5, 2008 (gmt 0)

10+ Year Member



Thanks,

The Error returned is a System Error -1072896658 on this line:

var response = http.responseText;

Like I said, my Javascript isn't good and I haven't got a clue about AJAX. If you can help me to understand it a bit more that would be great.