Create HTML Forms with Object-Oriented PHP
One of the things I really dislike about PHP is it’s reliance on spaghetti code. Every PHP developer has had to deal with <?php ?> tags strewn about HTML in a seeming mess. The worst situation is updating someone else’s code. Often it takes longer to decipher everything than it does to make the actual update.
I’ve been working on a project that takes a lot of user input from HTML forms and I decided at the beginning that I was not going to deal with a bunch of spaghetti code to get things done. Rather I wrote a serious of form classes in PHP5. These classes allow me to write HTML forms without having to write a single line of HTML — everything is handled in PHP and it has definitely made life easier.
Now I’m going to share the love so no one else has to suffer through a mess of convoluted code.
The Classes
My form class libary has seven classes. Each distinct form element type (e.g., <input>, <select>, etc.) is represented by its own class. All these elements are then pulled together in the Form class that handles the actual construction and ouputting of the form to the screen.
Download the Form class library here.
See the complete documentation for the Form class library here.
Download the examples here.
The Examples
So, you’re thinking, “this is cool, but can I see it in action?” Just read on.
A Simple Form
The first example is a simple form with one text input and a submit button.
// Create a new Form object
$form = new Form("ExampleForm");
// Add a text input
$form->addField(new FormInput("", "text", "firstname", "What is your name?"));
// Add a submit button
$form->addField(new FormInput("Submit", "submit", "submit"));
// Print the form to the screen
$form->printForm();
See the example in action.
A Simple Form with a Select
The second example adds a form select (drop down) menu, sets the default item in the select menu and shows off a different way to print forms.
// Create a new Form object
$form = new Form("ExampleForm");
// Add a text input
$form->addField(new FormInput("", "text", "firstname", "What is your name?"));
// Create some data for our select.
$genders = array();
$genders[] = array("value" => "M", // The value your script will see when the form is submitted.
"name" => "Man", // The name that will be displayed to users.
"selected" => false);
$genders[] = array("value" => "W",
"name" => "Woman",
"selected" => true); // Set this item to be selected by default
// Create a select.
$genderSelect = new FormSelect($genders, "gender", "Your are a: ");
// Add our select
$form->addField($genderSelect);
// Add the submit button.
$form->addField(new FormInput("Submit", "submit", "submit"));
// Save the form as an HTML string.
$html = $form->buildForm();
// Print the form later.
echo $html;
See the example in action.
A Less Simple Form with TextArea
This next example adds a textarea and provides some default text for it as well as the number of columns and rows.
// Create the text area. $defaultText = "Tell us about yourself. In 500 words or less."; $textarea = new FormTextArea($defaultText, "biography"); // You can also set properties like this. $textarea->setColumns(20); $textarea->setRows(5); // Add the text area. $form->addField($textarea);
See the example in action.
Organize Things with Separators
Our example form is starting to get big. Time to bring some organization into the mix. Enter separators. Separtors are purely organizational elements and will not be submitted along with your form. They simple allow us to visually break up the form so it’s easier to use.
// Add a separator with the text "Bio"
$form->addField(new FormSeparator("Bio")); // Want the separator without any text? Just take out "Bio" and you've got it.
See the example in action.
Block Bots with CAPTCHA
The final example adds CAPTCHA to our form. This class uses Securimage CAPTCHA and you’ll need to download it from here if you want to use the CAPTCHA features of this class library. The FormCaptcha object is a little more complicated than the rest so it’s easier to create a new object and set the properties afterward.
// Create a new FormCaptcha object.
// NOTE: the source you pass to the contructor is the URI. In other words,
// you could slap http://www.seanmonahan.org on front of the source and find that script.
$captcha = new FormCaptcha("/code/php/examples/form/Captcha/securimage_show.php");
// Position the reload text to be placed after the image.
$captcha->setReloadLocation("AfterImage");
// Set the label for the CAPTCHA input field.
$captcha->setLabel("Prove you aren't a bot.");
// Add the CAPTCHA to the form.
$form->addField($captcha);
See the example in action.