0

So I have this code :

function checkStatus()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {

if (xmlhttp.responseText == "1") {
document.getElementById("maincontent").innerHTML = '<br/><br/><center>Please refresh the page to continue.</b></center>';
}
    }
  }
xmlhttp.open("GET","file.php?id=1",true);
xmlhttp.send();
}

and I'm wondering about the last 2 lines (xmlhttp.open and xmlhttp.send) , what is the function of these ? also when i go to file file.php?id=1 using the browser it only displays "0" whereas the the general function of the code is to redirect me to a website after i do a specific action and i believe the data is stored on file.php?id=1 but how can i see it from browser ? Note: I'm not HTML/PHP programmer but i recognize the basics

2
  • You should add your PHP code if you want answer for your second question related to PHP Commented May 31, 2013 at 20:48
  • Are you really still supporting IE5 and IE6? (if so, you're mad; if not, you can simplify your code by dropping the ActiveX bit) Commented May 31, 2013 at 20:56

1 Answer 1

1

The lines before xmlhttp.open() just create the XMLHttpRequest object that will handle the AJAX connection. Calling xmlhttp.open() is necessary to actually open the connection and xmlhttp.send() to send the request. Only after the request is sent, a response can be received and handled by the onreadystatechange handler.

This code looks rather obsolete, however. I would recommend not using XMLHttpRequest directly but rather use a library for it - see jQuery, for example.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for help . Okey , I have a question , how can i display the response ? like using echo function to display it ?
for debugging purposes, just use window.alert(). For a real application you can use something like <div id="abc"></div> and JavaScript document.getElementById("abc").innerHTML="mytext". But you'd better post a new question for that to get more complete answers.
as I've stated above I'm not HTML/PHP programmer , so can you please help me a bit ? How can I know the where is the response defined ? so I put it window.alert(response) for example ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.