I’m using the ajax technique in combination with php and I want to know how to get the return variable from the function that is called by onreadstatechange.
A java function is called onsubmit to then call a php script to verify some things in my database and return true or false depending on the results.
Here is where I run into problems, I want the onsubmit=”return snappyajaxfunction()” to return false or true based on the results from the php.
My question is, how do I get the result of false or true from the stateChanged function to become the return for snappyajaxfunction.
I tried doing this for kicks: result = request.onreadystatechange=StateChanged with no luck.
I’ve also tried saving the responsetxt to a global variable then returning the global variable in snappyajaxfunction with no luck.
snappyajaxfunction()
{
request.onreadystatechange=stateChanged;
request.open("GET",url,true);
request.send(null);
return result;
}
function stateChanged()
{
if (request.readyState==4)
{
return request.responseText;
}
}
The purpose of this is to script is to verify the username / password entered by the user with the username / password stored in my database. If it they don’t match it returns false and the form will not submit, if it does the form submits and the user is brought to the welcome page…
You could do:
Where
OnStateChangedis a function that act as an event handler…EDIT: in
OnStateChangedyou can then submit or not your form based on responseEDIT2: On the server when you check the u/p, if they’re right log the user right on and return the status to JavaScript.. then in the JavaScript instead of resubmitting a form, just reload/redirect the page if it is successful or display an alert otherwise…
It is just not possible to return something from a function by an asynchronous operation in it… the original thread of execution is long gone. The asynchronous operation is executed by another thread (If I remember correctly, don’t flame).
Thanks