I'm doing a simple database validation. To avoid duplicate names, I want to see if the First and Last names are in the database and tell the user before writing the data. So I've got an ASP page that works fine. It returns the count of records where FName like 'xxx%' and LName like 'yyy%'. Running it alone it works great, so no problems there.
[COLOR=white blue]Here's the outline:[/color]
[blue]html:[/blue]
<button type='button' onclick='savePerson();'>
[blue]JS:[/blue]
function savePerson(){
var myTemp = checkIfNameExists(FName, LName);
alert(myTemp); [highlight #F3F3F3]// returns Undefined[/highlight]
if ( myTemp == true )
//notify user
else
//add person
}
function checkIfNameExists(FName, LName){
...
xmlHttp.onReadyStateChange = function() {
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
var sTemp = xmlHttp.responseText;
[highlight #F4F4F4]// an alert with sTemp here returns a valid value[/highlight]
return (sTemp > 0) ? true : false;
}
else { return false; }
}
}
xmlHttp.open("GET", url, false);
xmlHttp.send(null);
}
[COLOR=white blue]The problem:[/color]
What happens is checkIfNameExists() keeps coming back Undefined in the savePerson() call, but it looks fine in the Ajax call if status is 200. To me, it appears that what is happening is the first xmlHttp status is not 200 so the func returns false to my calling function and shortly afterward gets the 200 success, but by then, savePerson() has already received a false.
I would appreciate any thoughts.
[COLOR=white blue]Here's the outline:[/color]
[blue]html:[/blue]
<button type='button' onclick='savePerson();'>
[blue]JS:[/blue]
function savePerson(){
var myTemp = checkIfNameExists(FName, LName);
alert(myTemp); [highlight #F3F3F3]// returns Undefined[/highlight]
if ( myTemp == true )
//notify user
else
//add person
}
function checkIfNameExists(FName, LName){
...
xmlHttp.onReadyStateChange = function() {
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
var sTemp = xmlHttp.responseText;
[highlight #F4F4F4]// an alert with sTemp here returns a valid value[/highlight]
return (sTemp > 0) ? true : false;
}
else { return false; }
}
}
xmlHttp.open("GET", url, false);
xmlHttp.send(null);
}
[COLOR=white blue]The problem:[/color]
What happens is checkIfNameExists() keeps coming back Undefined in the savePerson() call, but it looks fine in the Ajax call if status is 200. To me, it appears that what is happening is the first xmlHttp status is not 200 so the func returns false to my calling function and shortly afterward gets the 200 success, but by then, savePerson() has already received a false.
I would appreciate any thoughts.