AJAX to validate data

B

Bruce A. Julseth

I'm new to AJAX and would appreciate some suggestions.

I want to use AJAX to search an existing database for information. If the
information exists, return true otherwise false. I am currently doing this
by submitting a php form and validating the data, then returning to then
reloading the client form with the true/false result. I have:

function validateStreet() {

var xmlhttp = getxmlhttp();
var serverPage = "validateStreet.php?street=";
var street = document.getElementById('Address').value;

serverPage += street;

xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
result = xmlhttp.responseText;
alert(result);
}
}
xmlhttp.send(null);

return result;

}

In this code, "result" is correct when displayed by the alert, but result is
"undefined" when "returned."

How do I return the "xmlhttp.responseText" value?

Thank you.

Bruce
 
M

Martin Honnen

Bruce said:
In this code, "result" is correct when displayed by the alert, but result is
"undefined" when "returned."

How do I return the "xmlhttp.responseText" value?

Well an 'A' in AJAX stands for 'asynchronous' as otherwise the HTTP
request would block the browser. So you need to process the response in
the onreadystatechange handler and use the result there, you can't
return a result from the function making the request.
 
B

Bruce A. Julseth

Martin Honnen said:
Well an 'A' in AJAX stands for 'asynchronous' as otherwise the HTTP
request would block the browser. So you need to process the response in
the onreadystatechange handler and use the result there, you can't return
a result from the function making the request.

Okay. I "Think" that explains why the alert works (that was within the
handler) but the return didn't. Right?

Thank you...
 
G

Gregor Kofler

Bruce A. Julseth meinte:
Okay. I "Think" that explains why the alert works (that was within the
handler) but the return didn't. Right?

Thank you...

Yes.

Gregor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top