I'm trying to make a registration page that tells the user whether the username they have selected is unique without reloading the page. The php script 'usernameAJAX.php' simply queries the sql database and returns a "1" if the username is not unique and a "0" if it is. For some reason, nothing is returned from the usernameAJAX(username) function i've created, not even the 'bool' variable set to "0" on the third line. Can someone explain why this is happening?
function usernameAJAX(username) {
var bool = "0";
params = "AJAXusername=" + username.value;
request = new ajaxRequest();
request.open("POST", "usernameAJAX.php", true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function()
{
if (this.readyState == 4)
if (this.status == 200)
if (this.responseText != null)
bool = this.responseText;
}
request.send(params);
function ajaxRequest()
{
try { var request = new XMLHttpRequest(); }
catch(e1) {
try { request = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e2) {
try { request = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e3) {
request = false;
} } }
return request;
}
return bool;
}