Close Search Box
Search Box

Search: From:

Close
Newsletter

9Tutorials to your Inbox



AJAX Generic Form Parser - With Validation

AJAX Generic Form Parser - With Validation
Author lv1 (2000/5000)
3,585 views
1 Star2 Star3Star4 Star5 Star (6 votes, average: 4.5 out of 5)

In this tutorial I’ll show you a simple method to pass any HTML form through AJAX without the need to hard code all form fields into the JavaScript or Server Side Script. Using this simple piece of JavaScript you can reuse it as is with any form, saving a lot of time. I’ve even added basic validation to certain form element types (which would be expected).

Okay, straight to the main JS code.

The JavaScript code above is split into 4 functions, here is an overview of what each function does.

createXMLHttpRequest() - This is the function which will establish the AJAX connection object, this is called as soon as the JS file is loaded.

sendRequest() - This function is the one which is called when the form is submitted. This function requires (passed in) the ‘Form Object’ and the ‘File Name’ of the script which will receive the form data. When this function has been called, it takes the form object and passes it to the JS function getForm() which in turn parses the whole form extracting all the data. sendRequest() then takes the open AJAX connection and passes all the data to the PHP file form processing. Finally when the request from the PHP server script is returned, it writes out the reply to an element on the screen with the div ID ‘results’.

getForm() - This is the magic, this function reads through the whole form and extracts the field data before returning it back to sendRequest(). It handles radio, text, password, textarea, select and checkbox field types. It even looks for validation requests on text, password and textarea fields (which is enabled by using title=�required� in the form element).

Simple :)

Next is a very simple form with a mixture of field types and validation requests (nothing fancy here). Also included at the bottom is the div which prints out the results to the browser.

As you can see the form trigger which calls the AJAX actions is set in the button using the onClick method. Also note I’ve added title=�required� to the ‘textOne’ field - this means the user will not be able to submit the form unless this field contains a value.

Finally here is a very stripped down Server script in PHP which takes the form values and returns them formatted back to the JS (you could do anything with the data at this stage!).

process.php

You could easily change the above to generate an email or add the form data to a database, the above example will echo out the results which are passed back to the div tag below the form.

There you have it, short and sweet (I hope) - I’ve intentionally kept the example scripts to a minumum but with still enough to be useful. Please feel free to take this and mess around with it.

Have Fun!!

Copyright @ Ajax Tutorial 2007

del.icio.us:AJAX Generic Form Parser - With Validation digg:AJAX Generic Form Parser - With Validation spurl:AJAX Generic Form Parser - With Validation newsvine:AJAX Generic Form Parser - With Validation blinklist:AJAX Generic Form Parser - With Validation furl:AJAX Generic Form Parser - With Validation reddit:AJAX Generic Form Parser - With Validation blogmarks:AJAX Generic Form Parser - With Validation Y!:AJAX Generic Form Parser - With Validation magnolia:AJAX Generic Form Parser - With Validation segnalo:AJAX Generic Form Parser - With Validation
Login




Friends' Sites

Contact Us

Categories

Sponsors

Featured Links

Post a Comment »








Safari hates me

Comment Guidelines

  • Hyperlinks are automatically generated.
  • <em>italic</em>
  • <strong>bold</strong>
  1. AJAX Generic Form Parser - With Validation | Best Web Design Resources. August 31, 2007

    [...] view plainprint? [...]

  2. Knowledge Base » Archive du blog » 9 tutorials » AJAX Generic Form Parser - With Validation September 11, 2007

    [...] 9 tutorials » AJAX Generic Form Parser - With Validation [...]

  3. Java Journals and Magazines, JSP, AjAX and Java « DGS about JAVA, Javascript July 28, 2008

    [...] AJAX, , How to Develop Web Applications with Ajax , Creating sortable lists with PHP and Ajax , AjAX Generic Form Parser , Jaxcent, Java-only AJAX programming JavaScript programming not required. [...]