PHP Class for Generating HTML Forms

The HTML_Form class is a basic php class for generating xhtml forms. It contains methods for generating common form elements such as text boxes, text areas, select lists, radio buttons, check boxes, and submit buttons.

View code for the PHP form class and an example.

To use the form class you include or require it in your file as follows:

require('includes/html_form.class.php');

With the file in place, the next step is to create an instance of the class:

$frm = new HTML_Form(); 

Form Class Methods

Each of the form class methods return a string which you can assign to a variable or echo/print.

The methods for adding form elements have named arguments for the required and most common attributes. You can use an optional associative array to add any other attributes you wish.

An addAttributes method is used to add the optional attributes passed in the associative array. It can be used to add class, JavaScript event handler attributes, or other attributes. In keeping with the requirements for xhtml validity, it does not output minimized boolean attributes, but instead writes out the full attribute-value pair.

Descriptions of the individual methods below demonstrate the addition of extra attributes.

Start and End Form

The form class includes methods for starting and ending a form: startForm and endForm.

$str = $frm->startForm('result.php');
// [form elements get added here]
$str .=  $frm->endForm();

startForm Arguments:

$action (string):
Destination URL to which form is submitted
$method (string):
Form method - get or post (default is post)
$id (string):
Unique id to be assigned to the form element's id attribute (optional)
$attr_ar (array):
Associative array of additional attributes (optional)

The following demonstrates use of startForm including the addition of class and onsubmit attributes:

echo $frm->startForm('result.php', 'post', 'myForm', 
    array('class'=>'myFormClass', 
      'onsubmit'=>'return checkBeforeSubmit(this)') );

addInput Method

The addInput method is used to add input elements of type text, checkbox, radio, hidden, password, submit and image. It has named arguments for type, name and value. A text box with just these attributes is added as follows:

$str = $frm->addInput('text', 'firstName', 'Sharon');

addInput Arguments:

$type (string):
Input element's type attribute value. Possible values: text, checkbox, radio, hidden, password, submit and image.
$name (string):
Value you specify is assigned to name attribute of the input element.
$value (string):
Value assigned to input element.
$attr_ar (array):
Associative array of additional attributes (optional)

The following demonstrates adding text boxes with additional properties included in the associative array:

echo $frm->addInput('text', 'firstName', 'Sharon', 
    array('id'=>'firstName', 'size'=>16, 'maxlength'=>50) );

echo $frm->addInput('text', 'total', '25.00', 
    array( 'size'=>10, 'class'=>'num', 'readonly'=>'readonly', 
        'onclick'=>'doSomething(this)') );

addLabelFor Method

The form class includes an addLabelFor method. To add a label element for the text box just demonstrated:

echo $frm->addLabelFor('firstName', 'First Name: ');

The addInput and addLabelFor methods result in the following display:

More addInput Examples

Radio buttons, checkboxes, submit and image buttons, hidden and password elements all use the addInput method:

// checkbox
echo $frm->addInput('checkbox', 'pref', 1);
// radio
echo $frm->addInput('radio', 'gender', 'male');
echo $frm->addInput('radio', 'gender', 'female');
// submit
echo $frm->addInput('submit', 'submitMyForm', 'Submit');

These can all be seen, along with the full source code, in the form class example.

addTextArea Method

To add a text area use the addTextArea method. Default values are provided for rows (4) and columns (30) or you can specify your own:

echo $frm->addTextArea('area', 6, 70);

addTextArea Arguments:

$name (string):
Assigned to name attribute of text area
$rows (integer):
Number of rows, controlling height of text area
$cols (integer):
Number of columns, controlling width of text area
$value (string):
Content of text area (optional)
$attr_ar (array):
Associative array of additional attributes (optional)

Select List Methods

The form class includes two methods for generating select lists: addSelectListArrays, used when option text and values are in separate arrays, and addSelectList, used when option text and values are both contained in a single array, or when the text will serve as value.

addSelectListArrays Arguments:

$name (string):
Assigned to name attribute of select list
$val_list (array):
Array containing option values
$txt_list (array):
Array containing option text
$selected_value (string):
Option value initially selected (optional)
$header (string):
Header displayed in select list with value emtpy (optional)
$attr_ar (array):
Associative array of additional attributes (optional)

An example use of addSelectListArrays:

echo $frm->addSelectListArrays('month', range(1, 12), 
        HTML_Form::$MONTHS_LONG, '', ' - Month - ');

addSelectList Arguments:

$name (string):
Assigned to name attribute of select list
$option_list (array):
Array containing option text, and optionally, values
$bVal (boolean):
Whether to include option value attributes
$selected_value (string):
Option value initially selected (optional)
$header (string):
Header displayed in select list with value emtpy (optional)
$attr_ar (array):
Associative array of additional attributes (optional)

An example use of addSelectList:

$rank_ar = array('Totally lame', 'Minimally useful', 
        'Pretty good', 'I realy like it', 'Fabulous');

echo $frm->addSelectList('rank', $rank_ar, true, 'Pretty good');

When the bVal argument is set true for a numerically indexed array, like $rank_ar, the values assigned to the option elements are 0, 1, 2, etc.

Example Form Using PHP Form Class

See the PHP form class example to view the full source code of the above form elements and more.

See the PHP Order Form for a demonstration of the form class used in combination with the PHP Table Class.

Back to top