Home arrow JavaScript arrow Page 2 - Building Slide Shows Using Progressive Enhancement
JAVASCRIPT

Building Slide Shows Using Progressive Enhancement


In this seventh part of the series, I develop a jQuery-driven slide show which uses the Cycle plug-in for performing the transitions between the sample images. While the fancy zooming effect applied to the images is carried out by the plug-in, it's worth noting that the images will remain visible even if the web page is displayed with JavaScript disabled. This demonstrates yet another case where Progressive Enhancement is applied with satisfying results.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
July 26, 2010
TABLE OF CONTENTS:
  1. · Building Slide Shows Using Progressive Enhancement
  2. · Review: using Progressive Enhancement when validating web forms
  3. · Building a simple slide show with jQuery
  4. · Adding behavior with jQuery's Cycle plug-in

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Building Slide Shows Using Progressive Enhancement - Review: using Progressive Enhancement when validating web forms
(Page 2 of 4 )

Just in case you haven't read the previous tutorial, which demonstrated how to use PE for building an accessible web form validation program, below I listed the source files corresponding to this example, so that you can examine them and understand how they work.

First, here's the web page that includes the target HTML form, along with the jQuery snippet that validates the inputted data and displays error messages via Ajax. Take a look at it:

<!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>

Simply put, the above code sample implements an enhanced version of this validation program, where the file responsible for checking the form data is called via the "ajax()" jQuery method and the errors raised during this process are echoed to the same web page.

Obviously, if JavaScript is disabled, this dynamic behavior is no longer available. The form is still validated via PHP, however, as its "action" attribute points to a file called "processform.php," which looks like this:

(processform.php)

<?php

// include 'Validator' class

require_once 'Validator.php';

$validator = new Validator;

// check entered form data

$validator->checkEmpty($_POST['fname'])

->checkEmpty($_POST['lname'])

->checkEmail($_POST['email']);

echo !$validator->validate() ?

$validator->getFormattedErrors() :

'<p>The data you entered is correct.</p>';

In this case, this earlier PHP file uses an instance of a class called "Validator" to check the data entered in the previous contact form. For the sake of completeness, below I include the definition of this class, which is as follows:

(Validator.php)

<?php

class Validator

{

protected $_errors = array();

protected $_errorPrefix = '<p>';

protected $_errorSufix = '</p>';

// check an empty string

public function checkEmpty($value)

{

$value = $this->sanitizeValue($value);

if ($value === '')

{

$this->_errors[] = 'The entered value must be a non-empty string.';

}

return $this;

}

// check an integer number

public function checkInteger($value, array $options = array())

{

$value = $this->sanitizeValue($value);

if (!filter_var($value, FILTER_VALIDATE_INT, $options))

{

$this->_errors[] = 'The value ' . $value . ' must be an integer number.';

}

return $this;

}

// check a float number

public function checkFloat($value, array $options = array())

{

$value = $this->sanitizeValue($value);

if (!filter_var($value, FILTER_VALIDATE_FLOAT, $options))

{

$this->_errors[] = 'The value '. $value . ' must be a float number.';

}

return $this;

}

// check an email address

public function checkEmail($value)

{

$value = $this->sanitizeValue($value);

if (!filter_var($value, FILTER_VALIDATE_EMAIL))

{

$this->_errors[] = 'The value '. $value . ' must be a valid email address.';

}

return $this;

}

// sanitize basically inputted value

protected function sanitizeValue($value)

{

return filter_var(trim($value), FILTER_SANITIZE_STRING);

}

// check the errors

public function validate()

{

return empty($this->_errors);

}

// get the error messages as an array

public function getErrors()

{

return $this->_errors;

}

// get the error messages as a formatted string

public function getFormattedErrors()

{

return $this->_errorPrefix .

implode($this->_errorSufix .

$this->_errorPrefix, $this->_errors) .

$this->_errorSufix;

}

// clean up error messages

public function clearErrors()

{

$this->_errors = array();

}

}

Done. At this point, you should have a clearer idea of how to utilize Progressive Enhancement for building truly accessible web form checking applications. But is that all that can be achieved with PE? Not by a long shot; as I said in the introduction, there's a plethora of cases where it's possible to take advantage of the functionality offered by PE. This includes the construction of engaging slide shows as well.

Therefore, in the following section I'm going to create the structural markup of the aforementioned slide show, which will be animated later in this article via the popular Cycle jQuery plug-in (http://jquery.malsup.com/cycle/).

Now, hurry up and read the lines to come!


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 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials