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.)
Using HTML Quickform for Form Processing (Page 1 of 13 )
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']] . '.'; }
Figure 3-1. HTML_QuickForm produces a form for the browser to display.
Figure 3.2. If you don't enter a title, an error is printed.
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.