Independent Digital

Creating and managing dynamic websites using Php, MySql, HTML and Wordpress with a bit of PEAR on the side

Entries Comments



Email This Post    Print This Post   

PEAR:HTML_QuickForm - a quickstart guide

28 November, 2006 (10:50) | PEAR | By: clive

Pear?s HTML_QuickForm makes the creation of forms very easy. The following quick-start guide assumes that you have successfully installed all the necessary files and that the installation was successful (for help with this, consult the many tutorials freely available on the web).

This is a very basic form covering most of the common fields required in a form. You can use the accompanying code to see how to include other element types.

Quickform picture

Have a look at the code, which can be downloaded here. It has detailed comments, which make the construction of the form very easy to understand.

When constructing a form you need to load the QuickForm class definitions using this line of code:

Require_once ?HTML/QuickForm.php?;

Then you need to instantiate an HTML_QuickForm object:

$form = new HTML_QuickForm(?form_name?, ?method?,?action?);

Here the method could be either GET or POST and the action is the html or php page that will actually use the data gathered by the form. By default it is the same page that contains the form ? the action is therefore optional. Then you add your form elements such as text boxes, radio buttons, etc. This is done with the following line that is repeated for each element:

$form->addElement(?element_type?,?element_name?,?label?,?attributes and/or values?);

You can then also add a validation rule which checks the input against the rule before the form is allowed to be submitted. For example, if the email input field is required, then the email validation rule will first check to see if this field has been completed before the form is processed. The client will not be able to submit the form until this field is completed. There are a number of rules supplied by QuickForm and you can also create your own. The validation can either be done on the client side if javascript is active or on the server side. The validation rule code looks like this:

$form->addRule(?element_name?,?Message to be displayed?,?rule?,?optional?,?server/client?);

You can also use filters to filter the input. For example by using the php string function trim, you can trim the input of all leading and trailing spaces. The code will look like this:

$form->applyFilter(?element_name?,?trim?);

Defaults can also be set by combining the element_name and default values in an array:

$defaults = array(’description’=>’the description of the thingy’,
‘radioset’=>’one’,
‘box_a’=>’checked’,
‘box_c’=>’checked’,
‘color’=>’black’,
’summary’=>’the default text’);

then, before displaying the form, you set the defaults by:

$form->setDefaults($defaults);

To display the form you use:

$form->display();

If you are using validation rules then you would display the form by using the following code:

//validate the form
if ($form->validate()) { //if the form validates then process the data
process();//calls process function (you can create this function to process the data) if it validates
} else { //displays the form if it does not validate ? will continue to do so until it validates
$form->display();
}

Quickform is very easy to use ? have a look at the downloadable code to see just how simple - yet it is also very powerful, enabling you to create some rather complicated forms if you wish. I hope that this quickstart guide helps you to get started quickly and easily enough to encourage you to want to learn more.

Have a look at our online Quickform FAQ reference guide. It’s free and online! Have a look now!

Bookmark and Share

Sorry, there are no related posts but check these out