469,602 Members | 1,912 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

using AJAX to do form validation

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

Mar 14 '07 #1
5 2990
lu*****@gmail.com wrote in news:1173903771.745900.182940
@n59g2000hsh.googlegroups.com:
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
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!!!!
Mar 14 '07 #2
On Mar 14, 4:50 pm, Good Man <h...@letsgo.comwrote:
lucy...@gmail.com wrote in news:1173903771.745900.182940
@n59g2000hsh.googlegroups.com:


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

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.

Mar 14 '07 #3
On 14 Mar, 21:08, lucy...@gmail.com wrote:
On Mar 14, 4:50 pm, Good Man <h...@letsgo.comwrote:
lucy...@gmail.com wrote in news:1173903771.745900.182940
@n59g2000hsh.googlegroups.com:
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
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.
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....

Mar 15 '07 #4
lu*****@gmail.com said the following on 3/14/2007 4:22 PM:
Hi,

I am trying to use XMLHttpRequest to do server side validation.
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.......

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 15 '07 #5
On Mar 15, 12:46 am, Randy Webb <HikksNotAtH...@aol.comwrote:
lucy...@gmail.com said the following on 3/14/2007 4:22 PM:
Hi,
I am trying to use XMLHttpRequest to do server side validation.

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.

Mar 15 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sunil | last post: by
5 posts views Thread by Martin | last post: by
1 post views Thread by pritisolank | last post: by
2 posts views Thread by goscottie | last post: by
pradeepjain
3 posts views Thread by pradeepjain | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.