I got bored last night and created this, and then used it, and I gotta tell ya...it saves you alot of time from typing!
<?PHP
/************************************************************
**
** File: formclass.php
** Description: Have you ever forgotten certain things
** when trying to make an html form? I know I have.
** This class will save you loads of time, and effort
** when creating forms.
**
** @ All form fields are XHTML Valid
** @ All except Start(), End(), and Input() require
** @ a name, and a value at the minimum.
** @ This script was created with Dreamweaver MX 2004
** @ and touched up with NotePad 2
** @ Code is Copyright (c) 2005 to TimTimTimma
** @ You may ONLY remove the above Copyright if you
** @ visit [url]http://timtimtimma.com[/url] and tell your friends
** @ about it. :-)
**
************************************************************/
class Form
{
var $action; var $method;
var $enctype; var $name;
var $target; var $randID;
var $sForm; var $Value;
var $Size; var $MaxLength;
var $Disabled; var $ReadOnly;
var $iInput; var $rRadio;
var $Checked; var $cCheckbox;
var $hHidden; var $bButton;
function Start($action = "", $method = "", $name = "", $target = "", $enctype = ""){
/************************************************************
**
** Name: Start Function
** Purpose: Creates the form settings and opens the
** <form> tag.
**
** @ By default, if all fields are left blank, a form
** @ name is randomly generated, the form is pointed
** @ at the page its currently residing at, and the
** @ 'get' method is used.
**
************************************************************/
if(!empty($enctype)){ $this->enctype = "enctype=\"".$enctype."\""; }else{ $this->enctype = ""; }
if(!empty($name)){ $this->name = "name=\"".$name."\""; }else{ $this->randID = rand(1,100); $this->name = "name=\"form_".$this->randID."\""; }
if(!empty($target)){ $this->target = "target=\"".$target."\""; }else{ $this->target = ""; }
if(!empty($action)){ $this->action = $action; }else{ $this->action = $_SERVER['PHP_SELF']; }
if(!empty($method)){ $this->method = $method; }else{ $this->method = "get"; }
$this->sForm = "<form action=\"".$this->action."\" method=\"".$this->method."\" ".$this->name." ".$this->target." ".$this->enctype.">\n";
return $this->sForm;
}
function End(){
return "</form>";
}
function Input($name, $type, $value = "", $size = "", $maxlength = "", $disabled = "" , $readonly = ""){
/************************************************************
**
** Name: Input Function
** Purpose: Help generate txt/ password fields.
**
************************************************************/
if(!empty($value)){ $this->Value = "value=\"".$value."\""; }else{ $this->Value = ""; }
if(!empty($size)){ $this->Size = "size=\"".$size."\""; }else{ $this->Size = ""; }
if(!empty($maxlength)){ $this->MaxLength = "maxlength=\"".$maxlength."\""; }else{ $this->MaxLength = ""; }
if(!empty($disabled)){ $this->Disabled = "disabled=\"true\""; }else{ $this->Disabled = ""; }
if(!empty($readonly)){ $this->ReadOnly = "readonly=\"true\""; }else{ $this->ReadOnly = ""; }
$this->iInput = "<input name=\"".$name."\" type=\"".$type."\" ".$this->Value." ".$this->Size." ".$this->iMaxLength." ".$this->Disabled." ".$this->ReadOnly." />\n";
return $this->iInput;
}
function Radio($name, $value, $checked = "", $disabled = ""){
/************************************************************
**
** Name: Radio Function
** Purpose: Help generate Radio fields.
**
************************************************************/
if(!empty($checked)){ $this->Checked = "checked"; }else{ $this->Checked = ""; }
if(!empty($disabled)){ $this->Disabled = "disabled=\"true\""; }else{ $this->Disabled = ""; }
$this->rRadio = "<input name=\"".$name."\" value=\"".$value."\" type=\"radio\" ".$this->Disabled." ".$this->Value." ".$this->Checked." />\n";
return $this->rRadio;
}
function Checkbox($name, $value, $checked = "", $disabled = ""){
/************************************************************
**
** Name: Checkbox Function
** Purpose: Help generate Checkboxes fields.
**
************************************************************/
if(!empty($checked)){ $this->Checked = "checked"; }else{ $this->Checked = ""; }
if(!empty($disabled)){ $this->Disabled = "disabled=\"true\""; }else{ $this->Disabled = ""; }
$this->cCheckbox = "<input name=\"".$name."\" value=\"".$value."\" type=\"checkbox\" ".$this->Disabled." ".$this->Checked." />\n";
echo $this->cCheckbox;
}
function Hidden($name, $value){
/************************************************************
**
** Name: Hidden Function
** Purpose: Help generate Hidden fields.
**
************************************************************/
$this->hHidden = "<input name=\"".$name."\" type=\"hidden\" value=\"".$value."\" />\n";
return $this->hHidden;
}
function Button($name, $value, $type = "", $disabled = ""){
/************************************************************
**
** Name: Button Function
** Purpose: Help generate different types of buttons.
**
** @ Type 1: Submit button
** @ Type 2: Reset button
** @ Type 3(default): Standard button (this is what will
** @ appear if you leave the other two blank or enter
** @ anything other then 1 or 2).
**
************************************************************/
if(!empty($disabled)){ $this->disabled = "disabled=\"true\""; }else{ $this->disabled = ""; }
if(!is_numeric($type) || $type > 2) $type = "";
switch ($type):
case 1:
$this->bButton = "<input name=\"".$name."\" type=\"submit\" value=\"".$value."\" ".$this->disabled." />\n";
break;
case 2:
$this->bButton = "<input name=\"".$name."\" type=\"reset\" value=\"".$value."\" ".$this->disabled." />\n";
break;
default:
$this->bButton = "<input name=\"".$name."\" type=\"button\" value=\"".$value."\" ".$this->disabled." />\n";
endswitch;
return $this->bButton;
}
}
/************************************************************
**
** @ Below is some sample code using the new class.
** @ Feel free to remove it.
**
** @ START SAMPLE CLASS CODE
**
************************************************************/
if(isset($_GET['submitted'])){
if(!empty($_GET['username'])){
echo "Hello there ".$_GET['username']."!";
}else{
echo "I'm sorry; I didn't get that. What did you say your name was again?";
}
}else{
echo "Hi there, my name is Tim, what is yours?";
}
$Form = new Form;
echo $Form->Start()
. $Form->Hidden("submitted", "1")
. "<p>Enter your name here: ". $Form->Input("username", "text", "Enter your name here", "25px", 50) ."</p>"
. "<p>".$Form->Button("Send", "Show me my name!", 1)."</p>"
. $Form->End();
/************************************************************
**
** @ END SAMPLE CLASS CODE
**
************************************************************/
?>