Code

Form Helper Class

The idea of this class is to eliminate a lot of the repetitious code that we always end up writing when constructing forms without having to load up an entire bulky framework. I started developing this back in May 2011 and am still working on it, so it will probably change some in the future, but I've already implemented in several places so I'll always try to keep new versions backwards compatible. If you come across any bugs or have suggestions, please leave them in the comments area.

To start, view and then download form.class.php. I've included a fair number of comments in there, so you can hopefully follow along easily. Then, you can read the documentation on the functions that I've included on this page. And, finally, you can check out this example (and view the code for it). Alternately, you can start with this blank form code.

start() · end() · process() · text() · radios() · checkboxes() · dropdown() · textarea() · date() · file() · hidden() · submit()

$form->start()

Starts the form and generates the opening <form> tag. Required.

Required Variables: None

Optional Variables:

$form->end()

Closes the form and generates the </form> tag. Required (because it includes hidden fields needed for form processing).

Required Variables: None

Optional Variables: None

$form->process()

Processes the submitted form and returns an array of data on success.

Required Variables: None

Optional Variables:

$form->text()

Generates a text or password input field.

Required Variables:

Optional Variables:

$form->radios()

Generates a set of radio fields.

Required Variables:

Optional Variables:

$form->checkboxes()

Generates a set of check boxes. Generates an array upon form submit unless combine_multiples is set in $form->process().

Required Variables:

Optional Variables:

$form->dropdown()

Generates a dropdown select element or a multiple select element.

Required Variables:

Optional Variables:

$form->textarea()

Generates a textarea field.

Required Variables:

Optional Variables:

$form->date()

Generates a set of dropdown select elements (and optionally a text field for the time) for the user to enter a date. Submitted value is translated into a UNIX timestamp.

Required Variables:

Optional Variables:

$form->file()

Generates a file upload field. If used, multipart needs to be set to TRUE in $form->start().

Required Variables:

Optional Variables:

$form->hidden()

Generates a hidden input field. Normally, you would just include these in $form-start(), but this is available in case you need it.

Required Variables:

Optional Variables:

$form->submit()

Generates a submit button.

Required Variables: None

Optional Variables:

Example Code

If you're like me, examples help a lot. So, I've constructed this basic example form, the code for which is displayed here:

<?php
$pagetitle = 'Example Ink Plant Form Helper Generated Form';
require 'header.inc';

echo "<h1>$pagetitle</h1>";

$GLOBALS['animals_options'] = array('b'=>'Bear','t'=>'Tiger','l'=>'Lion');

$GLOBALS['plants_options'] = array(
 'j'=>'Juniper Bush'
 ,'g'=>'Grass'
 ,'f'=>array('group'=>true,'caption'=>'Flowers','options'=>array('o'=>'Orchid','s'=>'Sunflower','r'=>'Rose'))
 ,'t'=>array('group'=>true,'caption'=>'Trees','options'=>array('p'=>'Palm Tree','pi'=>'Pine Tree'))
);


require_once DATA_DIR.'form.class.php';
$form = new Form(array('use_sessions'=>true));

$data = $form->process(array('form_name'=>'test','combine_multiples'=>', ','cleanup_function'=>'standard_cleanup'));
if ($form->success) {
    echo "<p>Form successfully submitted.</p>";
    echo "<ul>";
    foreach ($data as $key => $value) {
        echo "<li>$key = ".htmlspecialchars($value)."</li>";
    }
    echo "</ul>";
} else {
    echo $form->error_message;
    $hidden = array('testing'=>'yes','apple'=>'green');
    echo $form->start(array('name'=>'test','hidden'=>$hidden));
    echo "<table>";
    echo "<tr><th>Required Text</th><td>".$form->text(array('name'=>'field1','caption'=>'Field One','required'=>true))."</td></tr>";
    echo "<tr><th>Optional Text</th><td>".$form->text(array('name'=>'field2','caption'=>'Field Two','value'=>'something','required'=>false))."</td></tr>";
        $options = array('b'=>'Blue','g'=>'Green','y'=>'Yellow');
    echo "<tr><th>Radios</th><td>".$form->radios(array('name'=>'radio1','options'=>$options,'value'=>'b','required'=>true))."</td></tr>";
    echo "<tr><th>Radios Again</th><td>".$form->radios(array('name'=>'animals','value'=>'b','required'=>true))."</td></tr>";
    echo "<tr><th>Checkboxes</th><td>".$form->checkboxes(array('name'=>'check','caption'=>'Color Checks','options'=>$options,'value'=>array('b','y'),'required'=>true))."</td></tr>";
    echo "<tr><th>Dropdown</th><td>".$form->dropdown(array('name'=>'dd_single','options'=>$options,'blank'=>true))."</td></tr>";
    echo "<tr><th>Grouped Dropdown</th><td>".$form->dropdown(array('name'=>'plants','blank'=>true))."</td></tr>";
    echo "<tr><th>Dropdown Multiple</th><td>".$form->dropdown(array('name'=>'dd_multi','multiple'=>true,'size'=>3,'options'=>$options))."</td></tr>";
    echo "<tr><th>Date</th><td>".$form->date(array('name'=>'time'))."</td></tr>";
    echo "<tr><th>&nbsp;</th><td>".$form->submit(array('caption'=>'Submit Test'))."</td></tr>";
    echo "</table>";
    echo "<p>Here are the fields that are being used:</p>";
    echo $form->fields(array('form_name'=>'test','return_php'=>true));
    echo $form->end();
}

echo "<hr /><p>This page is an example for the article <a href=\"http://www.inkplant.com/code/form-helper.php\">Form Helper Class</a>. You can view the PHP code for it on that page.</p>" ;

require 'footer.inc';
?>

If you have questions (or suggestions), please leave them in the comments below.


0 Comments

Be the first to leave a comment.

Leave a Comment

Name
Email
Website
Comment
Name and email are required. Your email will not be published.

This post was published on Friday, July 8th, 2011 by Robert James Reese in the following categories: HTML, PHP. Before using any of the code or other content in this post, you must read and agree to our Terms & Conditions.

Copyright © 2013, Ink Plant. All rights reserved.