Home arrow JavaScript arrow Page 2 - Form Validation with Progressive Enhancement: Final Touches
JAVASCRIPT

Form Validation with Progressive Enhancement: Final Touches


In this sixth part of a series, I finish building a sample web application tasked with validating web forms via Ajax and PHP. The use of Progressive Enhancement is crucial to keeping the application working as expected, even if JavaScript is disabled on the browser.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
July 21, 2010
TABLE OF CONTENTS:
  1. · Form Validation with Progressive Enhancement: Final Touches
  2. · Review: building a form validation program using Progressive Enhancement
  3. · A basic data validation PHP class
  4. · A simple data checking script

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Form Validation with Progressive Enhancement: Final Touches - Review: building a form validation program using Progressive Enhancement
(Page 2 of 4 )

As I explained at the beginning, it’s feasible to use Progressive Enhancement during the construction of a program that validates HTML forms. In the previous part of the series, I developed the client-side part of this program. Below I included its source code, so that you can analyze it and understand how it works. Here it is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Processing a web form using Progressive Enhancement</title>

<script type="text/javascript" src="./js/jquery-1.3.2.min.js"></script>

<script type="text/javascript">

$(document).ready(function() {

$("form").submit(function (event) {

event.preventDefault();

$.ajax({

type: "POST",

url: "processform.php",

data: ({fname: $("#fname").val(), lname: $("#lname").val(), email: $("#email").val()}),

success: function(msg) {

$("#errors").html(msg);

}

});

});

});

</script>

<style type="text/css">

body {

padding: 0;

margin: 0;

background: #fff;

font: 0.8em Arial, Helvetica, sans-serif;

color: #000;

}

#wrapper {

width: 960px;

margin: 0 auto;

}

#header, #content, #footer {

padding: 20px;

}

</style>

</head>

<body>

<div id="wrapper">

<div id="header">

<h1>Processing a web form using Progressive Enhancement</h1>

<h2>Header section</h2>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>

</div>

<div id="content">

<div id="errors"></div>

<h2>Contact Form</h2>

<form action="processform.php" method="post">

<fieldset>

<p><label for="fname">First Name</label> <input type="text" name="fname" id="fname" /></p>

<p><label for="lname">Last Name</label> <input type="text" name="lname" id="lname" /></p>

<p><label for="email">Email Address</label> <input type="text" name="email" id="email" /></p>

<p><input type="submit" name="send" value="Send Data" /></p>

</fieldset>

</form>

</div>

<div id="footer">

<h2>Footer section</h2>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>

</div>

</div>

</body>

</html>

As you can see, the above web page includes a typical contact form which, when submitted, will pass its inputted data to a PHP file called “processform.php.” Even though its definition hasn’t been shown yet, the file's responsibility will be to validate the entered data and display the error messages that might arise during the checking process. In general terms, this could be considered the regular version of this sample program, which is entirely functional.

Things become more interesting, however, when JavaScript is enabled on the browser. In that case, the previous PHP file will be requested via the “ajax()” jQuery method, and the pertinent error messages will be shown on the same web page. See how easy it is to use PE when validating an HTML form? I bet you do.

Of course, the missing part of this program is the file that actually validates the form’s fields. In this case, the file will use a PHP class to perform this task, but identical results can be obtained with a procedural script.

The definition of this class will be discussed in the following section. To see how it will look now, jump forward and read the next few lines.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials