using AJAX to do form validation

L

lucyh3h

Hi,

I am trying to use XMLHttpRequest to do server side validation.
I have several fields on a form and a submit button. The submit button
has an event assocated with it when clicked. The javascript method
will do the form validation for each field one by one.
For each field, an XMLHttpRequst will be made to a PHP file and get
the return, either set an error field (<span>'s innerHTML) or leave it
empty.
Then I'll check the error field to see if it has been set to decide if
true or false should be returned for form submit button.
The problem I have now is, because XMLHttpRequest is asynchonous, I
sometimes check the error field BEFORE the request actually returned.
So I get wrong results.
So my questions are:
1. Is this the right way to do form validation using AJAX? Or I
shouldn't use AJAX to do form validation at all. I used to POST the
result to the page itself and do validation, but the page (PHP) gets
really complicated. Any suggestion?
2. For AJAX calls, is there any way to "wait" for it to finish?

Thanks!
Lucy
 
L

lucyh3h

(e-mail address removed) wrote in @n59g2000hsh.googlegroups.com:








1. This is not the right way to validate a form using AJAX... if you are
set on using AJAX, then assign an "onchange" event to each form field
you want to validate, and have that event/function call your AJAX stuff.
That way, the user knows whether or not a form field is 'valid' as soon
as they've entered data in the field... no need to wait until pressing a
'submit' button. And, needless to say, you must validate the data on
the server side (when the form is submitted) as well before doing
anything with it.

2. That defeats the whole point of AJAX. Asynchrounous!!!!- Hide quoted text -

- Show quoted text -

Thanks for the response! Guess I should keep the old way for doing
server side validation.
 
S

shimmyshack

Thanks for the response! Guess I should keep the old way for doing
server side validation.

hiya lucy, well there's a great book "ajax and php" by packt
publishers, and the code is a free download.
http://www.packtpub.com/files/code/1825_Code.zip
if it doesnt work for you, I'll mail you. Chapter 4 contains all the
code you will need for validation, including some SQL to help and so
on.. see what you make of it, it even includes a js "cache" to makes
ure that the routine gets all of the values....
 
S

shimmyshack

(e-mail address removed) said the following on 3/14/2007 4:22 PM:



You want to submit the form to the server, wait on the response, and
then use client side scripting to notify the user? Hell, just submit the
form.......
know what you mean!!
if one gets the server side extraction right, the same routines that
will take an ajax call will take a submission (if javascript is off)
which is the nice reason why this can all be unobtrusively written.
If you get it right it's almost web forms 2 in feel, but with really
effective enforcement.

I can see a benefit to this kind of interaction if you are writing a
mash-up, for instance I'm writing a system that allows user input to
be modified by searching over multiple databases and api's, feeds and
the like, based on the user input so far, it then applies some regular
expressions on a large text are which should contain "facts" the user
has copied and pasted or typed in, the whole thing kinda feeds off
itself in rather a nice way and ends up by prefilling some of the more
boring text boxes with appropriate best guess data.
I mean it aint google maps, but as well as validating the input it
does try to be more than ust a "place where stuff gets entered by the
user" - AJAX to the rescue - ahem.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top