How to return a variable from a function called onreadystatechange = function ()

I am using the ajax method in conjunction with php, and I want to know how to get the return variable from the function called onreadstatechange.

The java function is called onsubmit, and then it calls a php script to check some things in my database and return true or false depending on the results.

This is where I run into problems, I want onsubmit = "return snappyajaxfunction ()" to return false or true based on php results.

My question is: how to get the result false or true from stateChanged function to become a return for snappyajax function.

I tried to do this for kicks: result = request.onreadystatechange = StateChanged with no luck.

I also tried to save the responseetxt for the global variable and then return the global variable to 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 a script - check the username / password entered by the user with the username / password stored in my database. If it does not match, it returns false, and the form will not be submitted if it submits the form and the user is delivered to the welcome page ...

0
javascript ajax
source share
5 answers

You can do:

 function stateChanged() { if (request.readyState==4) { OnStateChanged(request.responseText); } } 

Where OnStateChanged is a function that acts as an event handler ...

EDIT: in OnStateChanged you can send or not form your form based on the response

EDIT2: on the server when you check u / p if they correctly register the user on the right and return the JavaScript status. then in JavaScript, instead of re-submitting the form, just reload / redirect the page if it is successful or displays a warning otherwise ...

It is simply impossible to return something from a function using an asynchronous operation in it ... the original thread of execution has long gone. Asynchronous operation is performed by another thread (if I remember correctly, do not flame).

thanks

+1
source share

jQuery will not automatically correct the fact that it is trying to base its view on the results of an asynchronous query.

What you can do is the following:

1) Set the onsubmit form to just call snappyajaxfunction ();

2) In stateChanged, if readistate is 4, get a link to the form and follow these steps:

 form.onsubmit = function() { return true; }; form.submit(); 

Basically, set onsubmit to always return true, and then force it to send again. For ease of use, you can disable the button that causes the sending, pending a change in state. And then turn it on again.

+1
source share

feel free to check out this tutorial on ajax.

It will show you how to use ajax step by step correctly

as inkedmn suggested, I would recommend using jquery or any other js structure

0
source share

The problem is that ajax calls are asynchronous. This means that the response is separate from the request. You must change your code to handle this. You cannot (should not) make synchronous http calls.

Take a look at this code. It works and you can read the answer:

 var req = new XMLHttpRequest(); req.open('GET', url, true); req.onreadystatechange = function (ev) { if (req.readyState == 4) { alert(req.responseText); } }; req.send(null); 
0
source share

To make your code, you can use closure and pass functions as parameters.
2 javascript properties are very good.

 snappyajaxfunction(url, fn) { var request... request.onreadystatechange=function() { if (request.readyState==4) { fn(request.responseText); } }; request.open("GET",url,true); request.send(null); } 

to use it you can build a function like

 var doSomething = function(response){ alert(response); }; snappyajaxfunction('/country/code/32', doSomething); 

Closing allows a "request" to be available inside a function that processes onreadystatechange. The 'fn' function is passed as a parameter to the snappyajax function.

0
source share

All Articles