I've got some javascript that checks over an html form for whatever issues there are that need to be corrected. However, the part that makes an ajax request back to a php script to check the database for presence of username doesn't work.
The php script queries the database and echoes back either "true" or "false".
The javascript function with the ajax request looks like this:
function checkUsername()
{
var username = document.getElementById("d_username");
var usernameValue = username.value;
var result;
var xmlhttp;
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)
{
result=xmlhttp.responseText;
if(result.trim() === "true"){
return true;
}else if(result.trim() === "false"){
return false;
}
}
}
xmlhttp.open("GET","scripts/checkUsername.php?username="+usernameValue,true);
xmlhttp.send();
}
The function that calls this calls a few other functions and does several checks on it's own. However, the part that call this function looks like this:
var check = checkUsername();
if(check===true){
messageText=messageText+"- Username already exists in system.<br />";
status = "fail";
}
However, I was originally using the function name right in the if statement like:
if(checkUsername()==true)
and if(checkUsername()=="true") plus many variations of ==s, ===s, ''s, ""s, all about the checkUsername() function and where it's called.
Could somebody please help me with why this isn't working.. I just want the function to be able to see if the php script returns true or false and then report either true or false respectively, then for the code that checks if it's true or false to act accordingly. Any help would be greatly appreciated!