Tired of authoring dull and dreary Web forms everyday? Fed up of programming JavaScript validations day-in and day-out? Then, the HTML_QuickForm package is just the solution for which you are looking. In the first part of this two-part tutorial, I'll get you started with the basics of building Web forms using this resourceful PEAR package.
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!