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 start with this blank form code.

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

__construct()

This loads up the Form class. And, by passing an array in here, you can set several options:

$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:

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 July 8th, 2011 and updated on March 9th, 2014 by Robert James Reese in the following categories: Helpers, HTML, PHP.

Before using any of the code or other content in this post, you must read and agree to our Terms & Conditions.

Copyright © 2014, Ink Plant. All rights reserved.