473,729 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3222
lu*****@gmail.c om wrote in news:1173903771 .745900.182940
@n59g2000hsh.go oglegroups.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.co mwrote:
lucy...@gmail.c om wrote in news:1173903771 .745900.182940
@n59g2000hsh.go oglegroups.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.c om wrote:
On Mar 14, 4:50 pm, Good Man <h...@letsgo.co mwrote:
lucy...@gmail.c om wrote in news:1173903771 .745900.182940
@n59g2000hsh.go oglegroups.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.c om 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.javas cript 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.c om 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
1204
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 the search button , validation throws error again, how can I prevent from this problem I mean when I click the search button,validation must return true ,because search elements and insert elements are in the same form.. <form id="Form5"...
3
1692
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 AJAX for these date validations. The validation must be done on onChange event of the date fields. What is the best way to do this using AJAX? Should i return XML data or JSON or just a simple text?
1
1871
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 way to validate a form field by field like AJAX would do, but to keep it synchronous? i.e. one action, one validation. Thanks,
5
2220
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 parts of your page. But in my application there are situation when I first notice on the server -- so AFTER sending an AJAX request -- that I have to update the complete page instead of only some parts. And what do I do now???
7
3617
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
1976
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 see some error popping up like uncaught exception ...just care to look in code nad let me know if u found something PHP file to handle resquest <?php /*This is a dummy code to handle the request*/ $method=$_POST; if($method=='POST') {
2
1544
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 Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
3
2112
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 improve that script with adding checkbox,radiobutton validation and dropdown list validation . but i am not able to do it.....any one can add his/her code into that ajax script..tht i can have a look at it.. And i have found a small bug in tht script...
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.