Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Controlling Online Forms with Builder Objects in PHP 5
  2. Defining a form element class
  3. Creating a form builder class
  4. Constructing a basic online form
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
October 11, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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>&nbsp;</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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: