. Developing Web Applications with Ajax, Part 4 - WebReference.com -

spacer
home / programming / javascript / jf / column15 / 1

Business Systems Analyst
Professional Technical Resources
US-OR-Portland

Justtechjobs.com Post A Job | Post A Resume
Developer News
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next

Developing Web Applications with Ajax, Pt. 4

In part four of this series of developing Web applications with Ajax, you'll learn how to submit information through forms without reloading the page. This can be used for a variety of purposes. Let's begin with the form's HTML, and then we'll move onto adding the JavaScript for submitting a form with Ajax.

Please keep in mind that throughout this article I will be using PHP to process server-side requests from JavaScript, so the code explained in this article will need to be saved as a file with a .PHP extension and must run on a PHP-enabled server. This will not work on your local hard disk.

This form submits to the following PHP file, ajax_output.php, which takes the data and outputs it.

The output is a DIV that contains a couple of paragraphs. If the name field is filled in, it will output "Hello, [your name]." If the hair field is filled in, it will say "You have [hair color] hair!" If you put in both, it will output both messages in the same DIV.

Now it's time for the JavaScript. Following is the JavaScript code and an explanation.

You first notice the JavaScript tag. This is a function that is evoked when the form is submitted (the onsubmit event handler in the form is what calls the function). The function begins by creating an XMLHttpRequest to send data to the form's action (ajax_output.php). Next, we loop through each and every form element and concatenate them with ampersands so that the data can be cleanly sent to the action. After that, we set up an onReadyStateChange function that populates the "insert" DIV with the data that ajax_output.php sends back. Then we open the form's method (ajax_output.php), either POST or GET (we're only set up for POST to work with our current ajax_output.php script) and the action (ajax_output.php). We set the request header so that JavaScript sends form urlencoded data to ajax_output.php, and then we tell JavaScript to send the form data to the action.

Conclusion

This script is very versatile and is useful in many situations, since you can put any form elements within the form above and the data will be POSTed to the form's action without reloading the page. You need simply modify the form's action file to reflect changes of input names in the form and how to handle that data.

About the Author

Jonathan Fenocchi is a Web developer who primarily works in the fields of Web Design with CSS, client-side scripting with JavaScript, and server-side scripting with PHP. His web site is located at: http://www.slightlyremarkable.com.

home / programming / javascript / jf / column15 / 1



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
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Choosing the Right Online Backup Provider · Mother Avaya Nurtures Her Technology Partners · Software as a Service a Winning Model for Hotspot Provider

Created: November 15, 2005
Revised July 19, 2006

URL: http://webreference.com/programming/javascript/jf/column15/1