Home arrow Graphic Design arrow Using HTML Quickform for Form Processing
GRAPHIC DESIGN

Using HTML Quickform for Form Processing


With HTML_QuickForm version 3.1, you can have a consistent look across all your forms and a simplified file upload. This chapter starts with the basics then shows you how to process submitted form data with HTML_QuickForm. (From the book, Essential PHP Tools: Modules, Extensions, and Accelerators, by David Sklar, Apress, 2004, ISBN: 159059280.)

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 178
September 01, 2004
TABLE OF CONTENTS:
  1. · Using HTML Quickform for Form Processing
  2. · Steps for the Example
  3. · Individual Elements
  4. · Text, Password, Textarea
  5. · Hidden, Select
  6. · Checkbox, Radio
  7. · Submit, Reset, Button, Image
  8. · File, advcheckbox, Static
  9. · Header, Link, HTML
  10. · Element Groups
  11. · Processing Submitted Data
  12. · Without a Callback Function
  13. · Setting Validation Rules

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Using HTML Quickform for Form Processing
(Page 1 of 13 )

sklar 

THE HTML_QUICKFORM module makes working with HTML forms easier. Instead of printing form elements one by one, you use its methods to define a form structure that you can print all at once. HTML_QuickForm automatically preserves defaults across form element submission, displays error messages, assures a consistent look to your forms, and simplifies file uploads.

This chapter describes HTML_QuickForm version 3.1. First, the chapter covers the basics of using HTML_QuickForm: the different form elements it supports and how to use them together in a form. It also explains element groups, which are a collection of related elements, such as two radio buttons that offer a Yes/No choice or three text boxes that hold the three parts (area code, exchange, and last four digits) of a phone number. Next, the chapter shows you how to process submitted form data with HTML_QuickForm, including how to handle uploaded files. Finally, the chapter details HTML_QuickForm’s validation capabilities. These let you check that the values submitted for specific form elements meet certain requirements, such as exceeding a particular length or matching a specific regular expression. HTML_QuickForm makes it easy to run these validation checks not only on the server once a form has been submitted but via JavaScript in the browser before the fields are submitted.

Creating and Displaying a Form

Usually, displaying a form in PHP is a sequence of print statements with logic to handle default values and loops to take care of tedious option lists for giant <select> menus that offer choices among all the days in a month or hours in the day. HTML_QuickForm works differently. The elements in a form are specified, in the order they should appear, and then the entire form is displayed at once. The following section details the basics of using HTML_QuickForm.

An HTML_QuickForm Example

This program uses HTML_QuickForm to display, validate, and process a simple form with three elements: a text box, a select box, and a submit button. When the form is submitted, the program checks to make sure that a value was typed in the text box. If so, it prints a message using the submitted data. If not, it redisplays the form with an error message. Figure 3-1 shows the rendered form. Figure 3-2 shows what happens if you submit the form without entering a value in the text box. Figure 3-3 shows what happens when you submit the form with avalue in the text box. The following is a simple example:

// Load the HTML_QuickForm module
require 'HTML/QuickForm.php';
// Instantiate a new form
$form = new HTML_QuickForm('book');
// Add a text box
$form->addElement('text','title','Book Title:');
// Add a select box
$subjects = array('Math','Ice Fishing','Anatomy');
$form->addElement('select','subject','Subject(s): ',$subjects);
// Add a submit button
$form->addElement('submit','save','Save Book');
// Add a validation rule: title is required
$form->addRule('title','Please Enter a Book Title','required');
// Call the processing function if the submitted form
// data is valid; otherwise, display the form
if ($form->validate()) {
$form->process('praise_book');
} else {
$form->display();
}
// Define a function to process the form data
function praise_book($v) {
global $subjects;
// Entity-encode any special characters in $v['title']
$v['title'] = htmlentities($v['title']);
print "<i>$v[title]</i> is a great book about ";
print $subjects[$v['subject']] . '.';
}

 sklar

Figure 3-1. HTML_QuickForm produces a form for the browser to display.

 sklar

Figure 3.2. If you don't enter a title, an error is printed.

 sklar

Figure 3-3. The praise_book() function prints a message when a title is submitted.

This chapter is from Essential PHP Tools: Modules, Extensions, and Accelerators, by David Sklar, (Apress, 2004, ISBN: 1590592808). Check it out at your favorite bookstore today.

Buy this book now.


blog comments powered by Disqus
GRAPHIC DESIGN ARTICLES

- Customizing Wordpress Favicons and Gravatars
- Building Corner Effects with Transparent Bac...
- 3D Graphics Technology: VRML Part I - Introd...
- Creating Visual Effects
- Web Page Design Overview
- Creating Artistic Photographs
- Working with Tools in Paint Shop Pro 8
- Using HTML Quickform for Form Processing
- Introduction to Adobe FrameMaker
- WebLogic Workshop, WebLogic Platform, and th...
- Planning the Site
- Working with Web Services
- WebLogic Workshop Application Development Ba...
- Scanning Images for Web Use
- Web Graphics Overview

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