Ajax Form Validation and Thread-safe Ajax

11/20/2009

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 your browser settings, AJAX validation on the client side will not work exposing sensitive data, and thus an evil to visitors the important data back) on the server (for example, damage caused by 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 side), when each form field loses focus (the onblur). The field's value sent to the server, which validates the data and return results (0 for failure, success 1). If the validation fails, an error message will quietly appear and notify the user of the validation failure.
* PHP style (on the server) you will be sent to the entire form. This is for a particular rule, if you check the user input validation is not always what server. If no errors, if found, the input data is valid, the browser will be redirected to a page successfully. If the validation fails, however, the form page the user is again stressed that sent the invalid field.

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

Some threads of the code will function correctly when it is safe when multiple concurrent threads. In this exercise, we have to make asynchronous requests to the server, the user input box and change every time you need to input data to validate the selected leaves.

The technology behind the hidden, but said, if the user input fields will soon be passed, or the server connection is slow, in this case, Web application will attempt to XMLHttpRequest object through a request for a new server is still busy to wait for a response in the previous request (which will generate an error, the application will not function properly).

Depending on the circumstances at hand, the ideal solution to this problem may be:

If you are new to all messages sent to the server must create an instance of the XMLHttpRequest *. To implement this method is simple, if multiple requests are sent to the same time, can degrade the performance of the server, it does not guarantee the order and receive a response.
* 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 automatic re-trial was made after the specified time requirements. This method is similar to a queue and you do not let more than one time server's request, but does not guarantee the order in which requests, or in the order the responses 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.

Queue is a first in first out (FIFO) structure, which guarantees the correct order to send e-mail. To obtain information on how to work, to demonstrate the feeling of pages and then press the TAB key to fast times, and then wait to see reaction to verify that the display one by one.

The complete AJAX and PHP tutorial features even more AJAX web development examples.
Downlaod code


PHP and Ajax submit form

Posted in: javascript tutorial| Tags: ajax client field side form user server demo validation thread-safe

Hot Posts

Latest posts

Tags

Others

Sponsors

asp.net interview questions