FormBuilder

FormBuilder is a PHP class that simplifies the process of creation and validation of HTML forms. It can also output the results in a text form and email them.

FormBuilder in essence manages a collection of field classes that implement a standard interface defined by the FormField class. Classes currently implemented include text fields, radio buttons, checkboxes, popup menus, date/time, etc.

The generated form can be fully customized through CSS.

The current version of FormBuilder is already very usable, but there are some improvements in planning: Better validation, more types of Fields and database integration.

Example

The best way to explain how FormBuilder works is to go through a simple example – a guest book:

// The first step is to include the FormBuilder class

require_once(‘FormBuilder.php’);

// Then FormBuilder needs to be initialized

form = new FormBuilder();

// insert some fields into the formform->addField(new TextField(‘Name:’, ‘name’,
25, 2, 30));
form->addField(new TextAreaField(‘Text:’, ‘textArea’,
1, 100, 5, 40));
form->addField(new SubmitField(‘submit’, ‘Send’));

// Next, check if the form has already been submitted,
// or if this is the first time the form is displayed

if (form->isSubmitted(‘submit’))
{
// Form has been submitted, so validate it.
form->validate();

if (form->isValid())
{
// Form is valid, display a success message
echo ‘Thank you for submitting the form!’;

// now the form content can be sent by email,
// displayed on screen, or stored in a database.
}
else
{
// Form is invalid, show an error message and
// display the form again

echo ‘The form is invalid, please correct the’.
‘ highlighted fields, and submit again’;form->display();
}
}
else
{
// First time the page is loaded, display the form
$form->display();
}