473,375 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,375 software developers and data experts.

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 3192
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ugurceng | last post by:
Hi, I have an aspx page that includes both search and insert screen , When I click the the insert button ,if required fields are blank then the validation.js throws an error, but when I click...
3
by: Sunil | last post by:
Hi, I have a from with around 8 date fields. All these are entered by the user and are to validated for holidays. The holiday validation can only be done on server side. I am planning to use...
1
by: AECL_DEV | last post by:
Hello Everyone, Ive seen alot of people saying that the best way to AJAX Validate a form is through the submit button, because validation should be synchronous. Im wondering, is there any good...
5
by: Martin | last post by:
Hello NG, I've been doing some AJAX for a few weeks now. The basics worked fine so far, but now I've got the following problem which I can't solve: With AJAX you typically update/replace only...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
1
by: pritisolank | last post by:
Hello Everyone, With that alert statment alert("calling validateme for "+document.form1.elements); in js things would work fine but if i remoe the form is simply getting submitted in firebug i can...
2
by: goscottie | last post by:
How do I prevent field validator which resides outside of UpdatePanel from firing off? The label will update correctly without RequiredFieldValidator via Ajax. TIA. Code below.... <%@ Page...
3
pradeepjain
by: pradeepjain | last post by:
hii guys , I wanna share a very gud ajax validation script with php... Ajax Form Validation - sForm | chains.ch weblog I am very new to ajax .So i wanna small help....i want to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.