search the site  

Enter search terms:


subscribe to newsletters   WebReference.com logo   WebReference.com
dev the Web
tip archive  •   about  •   contact  •   jobs  •   sitemap


[next]

How To Create Form Posts with Ajax

By Kris Hadlock

Digg This Add to del.icio.us

ASP 3.0/.NET Developer
Jupitermedia
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Now that Ajax is becoming a standard for handling interactions on the Web it's time to use it to update form submissions. In this article you'll learn how to create a reusable Ajax process which can be used with all forms. The source code for this article can be downloaded here and a live preview can be viewed here.

Starting With a Standard Web Form

A standard form includes a set of form tags with custom form fields, such as a name, city, state and so on. In order to successfully submit a form these form elements must have a unique name, so we can retrieve the value with a server-side language when the form is submitted. How we gather this information on the server-side is based on the method in which the form is sent, either a get or a post. Where we send it to is based on the action defined in the opening form tag and if it's left empty the page submits to itself. Listing 1 shows a standard Web form that submits to itself, uses the post method and sends a name and a message to the server on submission.

It's pretty straightforward and I'm sure that many of you know how to do this by now, but I think it's important to understand the basics when incorporating Ajax. In fact, if you understand how a standard Web form submits, Ajax can be incorporated without much effort. To do so, we need to catch the form submission and create the query string based on the values of the elements within the form.

Catching Form Posts

Catching a form submission is simple, all it requires is a forms onsubmit event. If we were to use the same form as before our code would look like listing 2.

The difference between this code and the standard Web form is that we're now including a couple JavaScript objects (which we'll discuss soon). We direct the form submission to a file called bridge.php and we catch the form submission with the onSubmit event where we call a JavaScript method and return a value of false, so the form doesn't submit as usual.

The two JavaScript objects that we include are Ajax and Post. The Ajax object is a custom object which I've created to handle Ajax requests and responses. (If you want to learn more about this object you can take a look at some of my beginning Ajax articles.) If you haven't downloaded the source code I've included the Ajax object code below.

The post object will be the focus of the rest of this article; it's used to catch form submission, build a query string from form element values and make an XMLHTTP request through the Ajax object.


[next]

Recent Articles

WebReference.com site name
Software Engineering for Ajax
Perl Pragma Primer
Implement Drag and Drop in Your Web Apps: Part 2
internet.com site name
Wi-FiPlanet Gift Guide, Class of 2008
Patriot Updates their DDR2 4GB PC2-8500 Line to "Revision 2"
Review: Samsung SCX-4500 Multifunction Printer




JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES