I'm trying to use javascript to check a MYSQL database after a user tabs out of a username field so as to notify if the name is in use or not. The function code I'm using is:
PHP Code:
function checkUser(str) { if (str=="") {return false;}
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) { document.getElementById("mainContent").innerHTML=xmlhttp.responseText; }
}
xmlhttp.open("GET","getuser.php?q="+str,true); xmlhttp.send(); }
This works, and I get the response from getuser.php placed into the mainContent DIV. However, I can not, for the life of me, get a return value. I understand the line that reads "xmlhttp.onreadystatechange=function() {blabla} and that any return values are probably returning to xmlhttp.onreadystatechange and not to the javascript that called the function "checkUser". But I can't figure out how to pull a return value. I'm calling this function like this:
PHP Code:
case "email": // verify valid email address, verify if address is in use, // colorize accordingly. If prohibited characters are found // here, then set field to red and set value to ""
var emailexists = showUser (field.value);
I have tried putting return true and return false in every point of the checkUser function that I can think of but I can never read that value from the case statement - it always comes up "undefined"
Is there a good way to do this? Since I've seen that the value is being read I am considering just making a hidden div and placing the information into that DIV and reading the DIVs contents from the case statement to determine if the username was found or not. I'm sure this would work but I would really like to do it "right" using a return value.
Anyone know what I need to do?
|