Using HTML_QuickForm To Manage Web Forms, Part 1 - Defining FORM Controls, Submitting Data
(Page 6 of 13 )
Next, we have the definition of the "checkbox", "submit" and "reset" <FORM> controls.
<?php
// snip
$obj_registration_form->addElement('checkbox', 'txtNewsletter', 'Subscribe to Newsletter:', 'Yes');
$obj_registration_form->addElement('html', '<tr><td colspan="2"> </td></tr>');
$obj_registration_form->addElement('submit', 'btnSubmit', 'Register');
$obj_registration_form->addElement('reset', 'btnReset', 'Start Again');
// snip
?>
Once again, I have invoked the resourceful addElement() method with the appropriate keywords - "checkbox", "submit" and "reset" - to add the required elements.
Next, I have to handle the submission of the Web form data itself. Since I have not specified any value for the "action" attribute of the <FORM> element, the page will post the data to itself when I click the "Register" submit button. Take a look at the next code snippet.
<?php
// snip
// filters before validation comes here
// validation rules come here
// validate form
if($obj_registration_form->validate()) {
// filters after validation comes here
// if data is valid, process form details
echo '<pre>';
var_dump($obj_registration_form->exportValues());
echo '</pre>';
// free the form values
$obj_registration_form->freeze();
}
// snip
?>
I have invoked the validate() method to verify the data entered by the user. Since I have not implemented any validation (more on this later), the form submission goes through without any hitch. Take a look at the output that you should see in your browser:

The above output display the values stored in the array returned by the exportValues() method. Next, I have called the freeze() method - this is used to "freeze" the Web form data, i.e. a user cannot modify the data once it has been frozen, as is evident from the output listed above.
<?php
// snip
// display the form
$obj_registration_form->display();
// snip
?>
The final act - I have called the display() method in order to render the Web form in the browser. If the Web form has been "frozen," this method will render the values submitted by the user, without the actual HTML <FORM> elements.
What I finally have is a fully functional Web form without typing a single line of HTML code in my PHP script!
Next: Grouping >>
More Design Usability Articles
More By Harish Kamath