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:
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