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
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