PHP AJAX Step By Step Tutorial - part 11: So far, we still don't review about server connection error handler. Now, we modify our code and add error handler about connection. Let's do it.
Open calc.html that we create at this post. Then modify process(funcCalc) become like this:
function process(funcCalc)
{
myDiv = document.getElementById("myDivElement");
myDiv.innerHTML = "";
// will be continued if xmlHttp isn't void
if(!xmlHttp) return;
if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4)
{
try
{
var param1 = document.getElementById("param1").value;
var param2 = document.getElementById("param2").value;
var params = "param1=" + param1 + "¶m2=" + param2 + "&func=" + funcCalc;
xmlHttp.open("Get","calc.php?" + params, true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
}
catch(e)
{
alert("Can't connect to server\n" + e.toString());
}
}
else
// don't try to make server request if the XMLHttpObject is busy
{
alert("Can't connect to server, please try again later.");
}
}