What is wrong with my ajax code?

TheNewDude

New member
I have made sure the url is correct but I can't open the connection. Could somebody give me an idea as to what is wrong with my code?

window.onload = getCategories;

var xhr;

function getCategories() {
xhr=GetXmlHttpObject();
if (xhr==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url = "categories.php";
url = url+"?sid="+Math.random();
xhr.onreadystatechange=stateChanged;
xhr.open("GET",url,true);
xhr.send(null);
}

function getSubcategories(id) {
xhr=GetXmlHttpObject();
if (xhr==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url = "subcategories.php";
var url = url+"?id="+id;
url = url+"&sid="+Math.random();
xhr.onreadystatechange=stateChangedTwo;
xhr.open("GET",url,true);
xhr.send(null);
}

function getSubsubcategories(id) {
xhr=GetXmlHttpObject();
if (xhr==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url = "subsubcategories.php";
var url = url+"?id="+id;
url = url+"&sid="+Math.random();
xhr.onreadystatechange=stateChangedThree;
xhr.open("GET",url,true);
xhr.send(null);
}

function stateChanged()
{
if (xhr.readyState==4)
{
document.getElementById("categories").innerHTML=xhr.responseText;
}
}

function stateChangedTwo()
{
if (xhr.readyState==4)
{
document.getElementById("subcategories").innerHTML=xhr.responseText;
}
}

function stateChangedThree()
{
if (xhr.readyState==4)
{
document.getElementById("subsubcategories").innerHTML=xhr.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
 
Back
Top