Here is the Form class. It may run out to the right a bit.
<?php
/*
##################################################
#
# Filename..........: $RCSfile: Form.php,v $
# Original Author...: Anthony L. Awtrey
# Version...........: $Revision: 0.1 $
# Last Modified By..: $Author: aawtrey $
# Last Modified.....: $Date: 2006/09/21 18:15:56 $
#
# Copyright 2006 Anthony Awtrey
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
*/
/*
* This class provides methods to build HTML forms.
*/
class Form {
var $action;
var $method;
var $hidden = array();
var $input = array();
var $radio = array();
var $submit = array();
var $reset;
var $form_label = '<label>%s</label>';
var $form_open = '<form action="%s" method="%s" enctype="multipart/form-data">';
var $form_hidden = '<input type="hidden" name="%s" value="%s" />';
var $form_text = '<input type="text" name="%s" value="%s" size="%s" maxlength="%s" />';
var $form_password = '<input type="password" name="%s" value="%s" size="%s" maxlength="%s" />';
var $form_file = '<input type="file" name="%s" value="%s" size="%s" maxlength="%s" />';
var $form_textarea = '<textarea name="%s" rows="%s" cols="%s">%s</textarea>';
var $form_select_open = '<select name="%s" size="%s">';
var $form_select_option = '<option value="%s" %s>%s</option>';
var $form_select_close = '</select>';
var $form_datepicker = '<input type="date" name="%s" value="%s" />';
var $form_checkbox = '<input type="checkbox" name="%s" %s />';
var $form_radio = '<input type="radio" name="%s" value="%s" %s />';
var $form_button = '<button name="%s">%s</button>';
var $form_close = '</form>';
/*
* Class initialization
*/
function __construct($action='index.php', $method='GET') {
global $_SERVER;
global $_SESSION;
global $_REQUEST;
global $_POST;
global $_GET;
$this->action = $action;
$this->method = $method;
}
function __destruct()
{
}
/*
* Add a hidden form input
*/
function hidden($name,$value) {
$c = count($this->hidden);
$this->hidden[$c]['name'] = $name;
$this->hidden[$c]['value'] = $value;
}
/*
* Add a text form input
*/
function text($label,$name,$value,$size='30',$maxlength='') {
$c = count($this->input);
$this->input[$c]['type'] = 'text';
$this->input[$c]['label'] = $label;
$this->input[$c]['name'] = $name;
$this->input[$c]['value'] = $value;
$this->input[$c]['size'] = $size;
if ( $maxlength == '' ) {
$this->input[$c]['maxlength'] = $size;
} else {
$this->input[$c]['maxlength'] = $maxlength;
}
}
/*
* Add a password form input
*/
function password($label,$name,$value,$size='30',$maxlength='') {
$c = count($this->input);
$this->input[$c]['type'] = 'password';
$this->input[$c]['label'] = $label;
$this->input[$c]['name'] = $name;
$this->input[$c]['value'] = $value;
$this->input[$c]['size'] = $size;
if ( $maxlength == '' ) {
$this->input[$c]['maxlength'] = $size;
} else {
$this->input[$c]['maxlength'] = $maxlength;
}
}
/*
* Add a file form input
*/
function file($label,$name,$value,$size='30',$maxlength='') {
$c = count($this->input);
$this->input[$c]['type'] = 'file';
$this->input[$c]['label'] = $label;
$this->input[$c]['name'] = $name;
$this->input[$c]['value'] = $value;
$this->input[$c]['size'] = $size;
if ( $maxlength == '' ) {
$this->input[$c]['maxlength'] = $size;
} else {
$this->input[$c]['maxlength'] = $maxlength;
}
}
/*
* Add a textarea form input
*/
function textarea($label,$name,$value,$rows='5',$cols='60') {
$c = count($this->input);
$this->input[$c]['type'] = 'textarea';
$this->input[$c]['label'] = $label;
$this->input[$c]['name'] = $name;
$this->input[$c]['value'] = $value;
$this->input[$c]['rows'] = $rows;
$this->input[$c]['cols'] = $cols;
}
/*
* Add a select form input
* $options must be a key/value array
*/
function select($label,$name,$options,$selected='',$size='1') {
$c = count($this->input);
$this->input[$c]['type'] = 'select';
$this->input[$c]['label'] = $label;
$this->input[$c]['name'] = $name;
$this->input[$c]['options'] = $options;
$this->input[$c]['selected'] = $selected;
$this->input[$c]['size'] = $size;
}
/*
* Add a date form input
*/
function datepicker($label,$name,$value) {
$c = count($this->input);
$this->input[$c]['type'] = 'datepicker';
$this->input[$c]['label'] = $label;
$this->input[$c]['name'] = $name;
$this->input[$c]['value'] = $value;
}
/*
* Add a checkbox form input
*/
function checkbox($label,$name,$checked=false) {
$c = count($this->input);
$this->input[$c]['type'] = 'checkbox';
$this->input[$c]['label'] = $label;
$this->input[$c]['name'] = $name;
if ($checked) {
$this->input[$c]['checked'] = 'checked';
} else {
$this->input[$c]['checked'] = '';
}
}
/*
* Add a radio form input
*/
function radio($label,$name,$value,$checked='') {
$c = count($this->radio);
$this->radio[$c]['type'] = 'radio';
$this->radio[$c]['label'] = $label;
$this->radio[$c]['name'] = $name;
$this->radio[$c]['value'] = $value;
$this->radio[$c]['checked'] = $checked;
}
/*
* Add a submit button
*/
function submit($name,$value) {
$c = count($this->submit);
$this->submit[$c]['name'] = $name;
$this->submit[$c]['value'] = $value;
}
/*
* Add a reset button
*/
function reset($name) {
$this->reset = $name;
}
/*
* Render the defined form into HTML
*/
function render() {
sprintf($format, $num, $location);
$output = sprintf($this->form_open."n", $this->action, $this->method);
foreach ($this->hidden as $key => $val) {
$output .= sprintf($this->form_hidden."n", $val['name'], $val['value']);
}
foreach ($this->input as $key => $val) {
switch ($val['type']) {
case 'text':
$output .= "<p>n";
$output .= sprintf($this->form_label."n", $val['label']);
$output .= sprintf($this->form_text."n", $val['name'], $val['value'], $val['size'], $val['maxlength']);
$output .= "</p>n";
break;
case 'password':
$output .= "<p>n";
$output .= sprintf($this->form_label."n", $val['label']);
$output .= sprintf($this->form_password."n", $val['name'], $val['value'], $val['size'], $val['maxlength']);
$output .= "</p>n";
break;
case 'file':
$output .= "<p>n";
$output .= sprintf($this->form_label."n", $val['label']);
$output .= sprintf($this->form_file."n", $val['name'], $val['value'], $val['size'], $val['maxlength']);
$output .= "</p>n";
break;
case 'textarea':
$output .= "<p>n";
$output .= sprintf($this->form_label."n", $val['label']);
$output .= sprintf($this->form_textarea."n", $val['name'], $val['rows'], $val['cols'], $val['value']);
$output .= "</p>n";
break;
case 'select':
$output .= "<p>n";
$output .= sprintf($this->form_label."n", $val['label']);
$output .= sprintf($this->form_select_open."n", $val['name'], $val['size']);
foreach ( $val['options'] as $k => $v) {
$selected = '';
if ( $k == $val['selected'] ) {
$selected = 'SELECTED';
}
$output .= sprintf($this->form_select_option."n", $k, $selected, $v );
}
$output .= $this->form_select_close."n";
$output .= "</p>n";
break;
case 'datepicker':
$output .= "<p>n";
$output .= sprintf($this->form_label."n", $val['label']);
$output .= sprintf($this->form_datepicker."n", $val['name'], $val['value']);
$output .= "</p>n";
break;
case 'checkbox':
$output .= "<p>n";
$output .= sprintf($this->form_label."n", $val['label']);
$output .= sprintf($this->form_checkbox."n", $val['name'], $val['checked']);
$output .= "</p>n";
break;
default:
break;
}
}
$output .= "<p>n";
foreach ($this->radio as $key => $val) {
$output .= sprintf($this->form_radio.$this->form_label."n", $val['name'], $val['value'], $val['checked'], $val['label']);
}
$output .= "</p>n";
$output .= "<p>n";
foreach ($this->submit as $key => $val) {
$output .= sprintf($this->form_button."n", $val['name'], $val['value']);
}
if ( $this->reset ) {
$output .= sprintf($this->form_button."n", 'reset', $this->reset);
}
$output .= "</p>n";
$output .= $this->form_close;
return $output;
}
}
?>