Sunday, 3 September 2017

javascript - Post Ajax function to return true or false

I'm trying to get an Ajax function to return true or false (it's a sort of validation function which is called from another function). So the other function needs to get a true or false value from this one so it would know what to do next




This is the function:



function validateLogin( uid, pass, i ){

var uvalue = uid.value;
var pvalue = pass.value;

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.open("POST","validateCheck.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");


xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{

if( xmlhttp.responseText == "Error" )
return false;
else if( xmlhttp.responseText == "Yes" )
return a;

else if( xmlhttp.responseText == 1 )
return false;
else if( xmlhttp.responseText == 2 )
return false;
else if( xmlhttp.responseText == 3 )
return false;
else if( xmlhttp.responseText == 4 )
return false;
else if( xmlhttp.responseText == 5 )
return false;

else
return true;
}
}

xmlhttp.send("uid="+uvalue+"&pass="+pvalue);
}


Calling function is:




function validateForm(i){
var uid = document.myform1.username;
var pass = document.myform1.password;

if( validateLogin(uid,pass,i) )
alert(validateLogin(uid,pass,i));

}



But the function validateLogin is not returning anything to the calling function(validateForm).

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...