HomePHP Page 4 - Controlling Online Forms with Builder Objects in PHP 5
Constructing a basic online form - PHP
Mastering some of the most popular design patterns with PHP 5 can be sometimes an overwhelming process that requires hard work and experience. However, if you want to tackle the challenge and expand your existing background on them, this article might eventually find a place on your quick reference list.
Basically, the example that I am just about to provide to you will illustrate the way that the form builder works, based on the class definition that you saw in the previous section. But what's the point of doing this, after all? Well, before I proceed to work directly with directors and builders, I want to demonstrate that all the objects involved in the creation process are fully functional.
Al right, assuming that all the classes have been placed inside the "classes" directory, a basic contact form might be programmatically built like this:
// generate sample online form
try{
// include class files
require_once('classes/form_element_class.php');
require_once('classes/form_builder_class.php');
// instantiate form builder object
$fb=new FormBuilder();
// add some form parts and elements
$fb->addFormPart('<table>');
$fb->addFormPart('<tr><td>First Name</td><td>');
$fb->addElement('text',array('name'=>'fname'));
$fb->addFormPart('</tr>');
$fb->addFormPart('<tr><td>Last Name</td><td>');
$fb->addElement('text',array('name'=>'lname'));
$fb->addFormPart('</tr>');
$fb->addFormPart('<tr><td>Email</td><td>');
$fb->addElement('text',array('name'=>'email'));
$fb->addFormPart('</tr>');
$fb->addFormPart('<tr><td>Comments</td><td>');
$fb->addElement('textarea',array('name'=>'comments',
'rows'=>'10','cols'=>'20'));
$fb->addFormPart('</tr>');
$fb->addFormPart('<tr><td> </td><td>');
$fb->addElement('submit',array('name'=>'send','value'=>
'Send Data'));
$fb->addFormPart('</tr></table>');
// display online form
echo $fb->getForm();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
In this case, the example listed above shows in a nutshell how the proper combination of only one form builder and some form element objects is good enough to create a sample contact form, which is composed of the following typical input boxes: First Name, Last Name, Email and Comments respectively.
After running the previous script, it neatly displays the form depicted below:
As you can see, the above screen shot demonstrates the excellent functionality provided by the form builder class, as well as by the respective form element objects, particularly when used in tandem.
At this stage, and after proving that the two first components required by the builder pattern work pretty well, the only thing that remains undone is logically creating the respective director class to construct online forms based on its directives.
However, I saved the best for the last, since this procedure will be covered in the last tutorial. In the meantime, have fun tweaking the code of all the classes shown here!
To wrap up
In this second part of the series, I showed you how to use the builder pattern in a more useful fashion: building online forms. Nevertheless, as I said before, the development process is still incomplete, since the corresponding director class hasn't been defined yet.
That's an excellent reason to catch the last part.See you then!