This code is a bit confusing, but let me get you started. Your xmlHttp variable is defined only within functions so it cannot be cross-accessed by functions (it goes out of scope). Instead, declare xmlHttp outside of all functions and then they'll share the variable's values. Next, you have
xmlHttp=GetXmlHttpObject() and within GetXmlHttpObject you start declaring xmlHttp again before returning it at the end. I have no idea what that kind of referencing will result in, but you probably want to try cleaning up the variable names.
That said, let me give you what we use on our site:
Code:
var request = false;
var requested_page = null;
function ajaxSend(url,data,form_type) {
if (form_type.length == 0) {
form_type = "GET";
}
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
if (!request) {
//Error
}
else {
//ajax commands available.
if (form_type == "POST") {
request.open(form_type, url, true);
request.onreadystatechange = ajaxReceive;
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send(data+"&ajax_call=true");
}
else {
request.open(form_type, url+"?"+data+"&ajax_call=true", true);
request.onreadystatechange = ajaxReceive;
request.send(null);
}
requested_page = data;
}
}
function ajaxReceive() {
if (request.readyState == 4) {
if (request.status == 200) { //everything went smoothly
var response = request.responseText;
receivePage(response);
}
// else an error accessing the URL.
}
// else still processing, so do not execute routine
}
To use, just call ajaxSend(url,data,form_type). For example, ajaxSend("some.asp","","GET").
Then, declare a function called receivePage that accepts one parameter - the return value - and performs the proper parsing. For example,
Code:
function receivePage(serverResponse) {
document.getElementById("myText").innerHTML=serverResponse;
}
As a last note, the code I gave you for our site does a bit more than you want but that's because it helps us to be able to determine if a call is ajaxian or not.
Hope this helps!