 |
Ajax Form Validation and Thread-safe Ajax
Author: Asif Khalyani  | Posted: 03-07-2008 | Comments: 0 | Views: 326 | Rating: (151) (?)
This degradable form validation demo verifies the form at the server side on the classic form submit, and also implements AJAX validation while the user navigates through the form.
Degradable AJAX Form Validation
Doing a final server-side validation when the form is submitted is always a must. If someone disables JavaScript in the browser settings, AJAX validation on the client side won't work, exposing sensitive data, and thereby allowing an evil-intended visitor to harm important data back on the server (for example, through SQL injection). Always validate user input on the server!
This form validation application validates a registration form, using both AJAX validation (client side) and typical server-side validation:
* AJAX-style (on the client), when each form field loses focus (onblur). The field's value is sent to the server, which validates the data and returns a result (0 for failure, 1 for success). If validation fails, an error message will unobtrusively show up and notify the user about the failed validation. * PHP-style (on the server), when the entire form is submitted. This is the usual validation you would do on the server, by checking user input against certain rules. If no errors are found and the input data is valid, the browser is redirected to a success page. If validation fails, however, the user is sent back to the form page with the invalid fields highlighted.
Both AJAX validation and PHP validation check the entered data against these rules:
* Username must not already exist in the database * Name field cannot be empty * A gender must be selected * Email address must be written in a valid email format, such as xyz@domain.com * Phone number must be written in standard US form: +xx xxxxxxxxx Thread-Safe AJAX
A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads. In this exercise, we need to make an asynchronous request to the server to validate the entered data every time the user leaves an input box or changes a selection.
The hidden danger behind this technique is only revealed if the user moves very quickly through the input fields, or the server connection is slow; in these cases, the web application would attempt to make new server requests through an XMLHttpRequest object that is still busy waiting for the response to a previous request (this would generate an error and the application would stop functioning properly).
Depending on the circumstances at hand, the ideal solution to this problem may be:
* Create a new XMLHttpRequest instance for every message you need to send to the server. This method is easy to implement, but it can degrade server's performance if multiple requests are sent at the same time, and it doesn't guarantee for the order in which you receive the responses. * Record the message in a queue and send it later when the XMLHttpRequest object is able to make new requests. The requests are made in the expected order. Using a queue is particularly important in applications where the order of the messages is important. * Schedule to automatically retry making the request after a specified amount of time. This method is similar to the one with the queue in that you don't make more than one server request at a time, but it doesn't guarantee for either the order in which the requests are made, or for the order in which the responses are received. * Ignore the message.
In this form validation exercise, we use a message queue. When the user leaves an input element, a message to validate its value is added to the queue. When the XMLHttpRequest object is clear to make a new request, it takes the first message from the queue.
The queue is a First-In, First-Out (FIFO) structure, which guarantees that the messages are sent in the proper order. To get a feeling about how this works, go to the demo page, and press tab quickly multiple times, and then wait to see how the validation responses show up one by one.
The complete AJAX and PHP tutorial features even more AJAX web development examples. Downlaod code
PHP and Ajax submit form
Rate this Article:
Current: 4.5 / 5 stars - 4 vote(s).
Article Source: http://www.articlesbase.com/programming-articles/ajax-form-validation-and-threadsafe-ajax-471466.html
About the Author:I am Asif Khalyani. I am software Engineer. PHP and ajax free script download site phpasks. User can download free php script.
|
Submitting articles has become one of the most popular means to drive traffic to your website and promote yourself and your business. Join us today - It's Free! |
|
Related Articles
Upload Image Without Refresh Page - Asynchronous Image File Upload Without Ajax By: Asif Khalyani | 23/06/2008 | Programming I suppose it is neccessary to bring a little bad news to Ajax at this point, it is not possible to process a file upload through the XMLHtrtpRequest Object. The reason of this is that javascript has no access to your computer's file system.
Crop Image File Size, Width & Height Using Php Script By: Asif Khalyani | 03/07/2008 | Programming Cropping and resizing your images for the Web is a common technique for creating smaller thumbnail images that download quickly.
Ajax Rating Script - Php & Mysql By: Asif Khalyani | 09/09/2008 | Programming Frequent visitors of ajax enabled websites, like ajaxian, have all witnessed them already: ajax rating widgets.
Easy Ajax Inline Text Edit 2.0 By: Asif Khalyani | 09/09/2008 | Programming A small piece of javascript reads al SPAN tags, checks if it has class="editText" and a id=. If that is true, it adds a onclick function. That onclick function will create a textfield or input (depending on the size of the editable text). Someone has the ability to edit the field. When the text field is blurred, it will read the contents, and starts a XMLHttpRequest and ‘sends’ the content + fieldname + any set vars to an update file.
By: | 31/12/1969 |
Rich Internet Applications (ria) By: Max J. Pucher | 22/03/2008 | Information Technology The all-new hype is Rich Internet Applications (RIA). Adobe is touting Flash FLEX functionality as the next big thing in RIA. Papyrus EYE makes the development of completely dynamic user frontends in the browser much easier than Flex.
Creating a Simple Ajax Control in Asp.net 2.0 and C# By: Programminghelp.com | 08/08/2008 | Programming In this article I will explain how you can easily create an AJAX enabled web site within Visual Studio 2005 utilizing C# and how to utilize the AJAX framework to add a simple control from the list of controls available in the AJAX Control Toolkit.
The Web 2.0 Effect: the Characteristics of a Web2.0 Website By: M.Markell | 05/02/2007 | Networks The term web2.0 was originally presented by O'Reilly Media (A well known media company publishing books and websites on various computer technology topics). It's a term that refers to a new generation of websites (social networking websites, wiki-based websites etc). These websites take advantage of web application technologies and give web users the ability to collaborate and share their experiences, views, opinions and interests while they surf the web.
Got a Question? Ask.
Ask the community a question about this article:
Frequently Asked Questions
Ajax Hot Water Boilers
By: ralphepinc | 01-07-2008
I would appreciate any information about fires caused by,or connected with, Ajax hot water boilers.
What Language to write software?
By: Mr. Barber | 08-03-2008
I WANT THE ULTIMATE SMART HOME ;) For free. * What language should I use to write a sophisticated home automation solution? I think I might strongly prefer Java desktop programming. I know NOTHING of programming, sadly. I would need this in my home automation program...remote desktoping.free and owned by me and open source.voice chat bot running aiml.control over the electrical switches of my home.control of my remotes, tvs, computers, and electronics.be able to call my cell phone and send a txt message.motion web cam. I want to run all this on a desktop machine in my cieling, remote desktoped to linux computers throughout the home. Especially for when I'm away on business, I want to be able to vocally ask my house fluid human-sounding questions. A program called DragonSpeak might help me. Text-To-Speech, too. And an AIML chatbot. "Turn off the lights, put on music, forward a call, turn on Bob's PC, etc." Has anyone seen the TV show Eureka? Well they have a talking house. I want that. I know I can make that. So in review... I think Java might solve all my problems. Flexible, powerful, and very supported. I'm very much open to alternatives, though. Such as Google Open Handset Alliance having their "Android" os that might be fun to program for. Google also has a bunch of code databases to browse through code to see what it does. So is Java the multipurpose way of the future for home automation?/security/AI I really know nothing of the topic. What a messy question I've asked...
What OS to learn Java on? Ubuntu or Vista?
By: Mr. Barber | 08-03-2008
Is it easier to learn/practice Java desktop programming in Linux or Windows? I'm talking about really large complex desktop software. Possibly 3D. Not web development...well maybe a little.I'm aware there are a ton of other languages and options out there. C++, .NET, Cobalt, AJAX, Flash, Silverlight, on and on and on and on....... :P I almost don't even care about those, I just want Java, being OS independent, and has a bright future ahead.
Can javascript be implemented in PHP?
By: xxAlex | 05-02-2008
Can javascript be implemented in PHP?
What is the correct way to build ajax based user interface for a website?
By: rogerbit | 20-08-2007
Do standart exist? Where can I read about it?
'the script running on this page cause the internet explorer to run slowly,if it continues to run your computer may become unresponsive.
By: Swati_Jain | 04-05-2007
Hello All,I am trying to open the popup panel on click of link button of grid using popup control extender control of ajax cotrol toolkit but when a run the application i get the error 'the script running on this page cause the internet explorer to run slowly,if it continues to run your computer may become unresponsive.My code is as follows .What could be the possible solution for this?
Q&A Powered by:
Latest Programming Articles
Tips for Buying Software Online By: Daniel Jowssey | 17/11/2008 Buying software online not only helps save the planet, it also has other benefits, including:
* Ease and Simplicity. You can purchase software in your underwear at 4am if you really want to. Shopping online doesn’t have to be done within regular business hours, nor do you need to look your best to do it. It’s also easy to shop around for the best prices and takes less time than driving to the shops.
Mvc Design Pattern By: TuVinhSoft .,JSC | 14/11/2008 Model-view-controller (MVC) is an architectural pattern used in software engineering. In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (View) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.
Advantages of Low Cost Contract Programmers in Freelance Programming By: Joanna Gadel | 12/11/2008 It observed that web industry is getting tougher thus the necessity of freelance contract programmer is required for developing more effective website with flexible features. This article states the fruitful advantages of freelance programmers in contract programming.
A Guide to Cnc Kits By: Martin Applebaum | 09/11/2008 CNC kits are a way in which to construct your CNC machine. This article will provide some information on these machines.
A Guide to Cnc Tube Bending Machines By: Martin Applebaum | 08/11/2008 Are you familiar with a CNC tube bending machine? This article will shed some light on the main function and components of this machine.
Ways to Hire Dedicated Php Programmers By: Jucick | 08/11/2008 It’s not at all easy to hire dedicated PHP programmers unless you know where and how to find them. Whether you need to fix, update or enhance your website you naturally want the job done quick and right.
Top 4 Reasons Why Addressing Web Accessibility is Important By: Matt Cave | 05/11/2008 There are very high chances that web accessibility is more important to the performance of your web site than you realize. Article takes a look at the top 4 reasons why it would be important to address the issue of web accessibility.
Xlphoto Printing, the Best Choice for Banner Printing and Poster Printing By: Vikram Kumar | 01/11/2008 Suppose, you have visited a tourist spot or a hill station; over there, you have witnessed some of the mesmerizing locations of serene nature.
More from Asif Khalyani
Easy Ajax Inline Text Edit 2.0 By: Asif Khalyani | 09/09/2008 | Programming A small piece of javascript reads al SPAN tags, checks if it has class="editText" and a id=. If that is true, it adds a onclick function. That onclick function will create a textfield or input (depending on the size of the editable text). Someone has the ability to edit the field. When the text field is blurred, it will read the contents, and starts a XMLHttpRequest and ‘sends’ the content + fieldname + any set vars to an update file.
Ajax Rating Script - Php & Mysql By: Asif Khalyani | 09/09/2008 | Programming Frequent visitors of ajax enabled websites, like ajaxian, have all witnessed them already: ajax rating widgets.
Crop Image File Size, Width & Height Using Php Script By: Asif Khalyani | 03/07/2008 | Programming Cropping and resizing your images for the Web is a common technique for creating smaller thumbnail images that download quickly.
Upload Image Without Refresh Page - Asynchronous Image File Upload Without Ajax By: Asif Khalyani | 23/06/2008 | Programming I suppose it is neccessary to bring a little bad news to Ajax at this point, it is not possible to process a file upload through the XMLHtrtpRequest Object. The reason of this is that javascript has no access to your computer's file system.
|
 |