Portfolio
Scripts and Websites I've made using PHP and MySQL mainly.
Snippets
Code snippets and mini tutorials
Contact
Contact me if you have any questions or queries
Home / Snippets / forms_with_php
This section is new and undergoing snippet addition, as of 13/6/11
A class to create forms with PHP.
You start off by creating the form object, then you add fields to the form using the addField() method. The parameters sent should be: the name of the field, the type of the field, any additional attributes, any options (if it's a select field).
The form can either be printed all together using the printForm() method and you can style the output with CSS. Or you can print each field individually using the printField() method and specifying the name of the element.
E.g. - Using printForm()
This would give you HTML output of:
E.g. - Using printField()
";
echo "
";
echo $myForm->footer;
This would give you HTML output of:
Types of form elements you can add:
This section is new and undergoing snippet addition, as of 13/6/11
Forms with PHP
A class to create forms with PHP.
/*
* Creating HTML forms with PHP
*
* @author Conn Warwicker
*/
class PHPForm
{
public $id; // The id attribute of the form
public $name; // name attribute of the form
public $action; // action attribute of the form
public $method; // method attribute of the form
public $header; // The <form> section of the HTML
public $footer; // The </form> section of the HTML
public $fields = array(); // An array of the elements added to the form
public $num; // Number of elements added, to be used as unique ID if >1 elements have the same name attribute
public $printed = array(); // Array of the elements which have already been printed out
/*
* Construct PHPForm Object
* @param $a The action, e.g. "file.php"
* @param $m The method, e.g. "post"
* @param $i="" The id of the form, e.g. id="something" (Optional)
* @param $n="" The name of the form, e.g. name="something" (Optional)
*/
public function __construct($a, $m, $i="", $n="")
{
$this->action = $a;
$this->method = $m;
$this->id = $i;
$this->name = $n;
$this->num = 1;
$this->header = "<form action=\"".$this->action."\" method=\"".$this->method."\" id=\"".$this->id."\" name=\"".$this->name."\">\n";
$this->footer = "</form>\n";
}
/*
* Add a field to the form
* @param $name The name of the element, e.g. name="something"
* @param $type The type of element, e.g. "text", "password", etc...
* @param $attributes="" An array of any additional attributes to be added to the element, e.g. "maxlength='10'"
* @param $options="" An array of any <option>s to be added to the element if it is a <select> element
*/
public function addField($name, $type, $attributes="", $options="")
{
$att = "";
if(is_array($attributes))
{
foreach($attributes as $key => $v)
{
$att .= $key . "=\"".$v."\" ";
}
}
$this->fields[] = array("name" => $name, "type" => $type, "att" => $att, "opt" => $options, "ID" => $this->num);
$this->num++;
}
/*
* Print the whole form
*/
public function printForm()
{
echo $this->header;
for($i = 0; $i < count($this->fields); $i++)
{
$this->printField($this->fields[$i]['name']);
}
echo $this->footer;
}
/*
* Print a specific element/field based on the name provided
* @param $name The name of the element to print, e.g. <input type="text" name="i" /> in this case you would want to call printField("i");
*/
public function printField($name)
{
// Find Element In Array
$i = 0;
for($j = 0; $j < count($this->fields); $j++)
{
if($this->fields[$j]['name'] == $name && !in_array($this->fields[$j]['ID'], $this->printed))
{
$i = $j;
break;
}
}
switch($this->fields[$i]['type'])
{
case 'text':
echo "<input type=\"text\" name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."/>\n";
break;
case 'password':
echo "<input type=\"password\" name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."/>\n";
break;
case 'textarea':
echo "<textarea name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."></textarea>\n";
break;
case 'checkbox':
echo "<input type=\"checkbox\" name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."/>\n";
break;
case 'radio':
echo "<input type=\"radio\" name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."/>\n";
break;
case 'select':
echo "<select name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att'].">\n";
foreach($this->fields[$i]['opt'] as $key => $val)
{
echo "<option name=\"".$key."\">".$val."</option>\n";
}
echo "</select>\n";
break;
case 'multiple':
echo "<select multiple name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att'].">\n";
foreach($this->fields[$i]['opt'] as $key => $val)
{
echo "<option name=\"".$key."\">".$val."</option>\n";
}
echo "</select>\n";
break;
case 'hidden':
echo "<input type=\"hidden\" name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."/>\n";
break;
case 'file':
echo "<input type=\"file\" name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."/>\n";
break;
case 'button':
echo "<input type=\"button\" name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."/>\n";
break;
case 'submit':
echo "<input type=\"submit\" name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."/>\n";
break;
case 'image':
echo "<input type=\"image\" name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."/>\n";
break;
case 'reset':
echo "<input type=\"reset\" name=\"".$this->fields[$i]['name']."\" ".$this->fields[$i]['att']."/>\n";
break;
}
array_push($this->printed, $this->fields[$i]['ID']);
}
}
You start off by creating the form object, then you add fields to the form using the addField() method. The parameters sent should be: the name of the field, the type of the field, any additional attributes, any options (if it's a select field).
The form can either be printed all together using the printForm() method and you can style the output with CSS. Or you can print each field individually using the printField() method and specifying the name of the element.
E.g. - Using printForm()
$form = new PHPForm("", "get", "myForm", "myForm");
$form->addField("f_1", "text");
$form->addField("f_2", "password", array("maxlength"=>10));
$form->addField("s_1", "submit");
$form->printForm();
This would give you HTML output of:
<form action="" method="get" id="myForm" name="myForm"> <input type="text" name="f_1" /> <input type="password" name="f_2" maxlength="10" /> <input type="submit" name="s_1" /> </form>
E.g. - Using printField()
$myForm = new PHPForm("somefile.php", "post");
$myForm->addField("f_1", "text");
$myForm->addField("f_2", "password", array("maxlength"=>10));
$myForm->addField("s_1", "submit", array("style"=>"border:none;background-color:#000;color:#fff;", "value"=>"CLICK"));
echo $myForm->header;
echo "| Text Field: | "; $myForm->printField("f_1"); echo " |
| Password Field: | "; $myForm->printField("f_2"); echo " |
| "; $myForm->printField("s_1"); echo " |
This would give you HTML output of:
<form action="somefile.php" method="post" id="" name=""> <table><tr><td>Text Field:</td><td><input type="text" name="f_1" /> </td></tr><tr><td>Password Field:</td><td><input type="password" name="f_2" maxlength="10" /> </td></tr><tr><td><input type="submit" name="s_1" style="border:none;background-color:#000;color:#fff;" value="CLICK" /> </td><td><br /></td></tr></table></form>
Types of form elements you can add:
- Text Input - "text"
- Password Input - "password"
- Textarea - "textarea"
- Checkbox Input - "checkbox"
- Radio Input - "radio"
- Select - "select"
- Multiple Select - "multiple"
- Hidden Input - "hidden"
- File Input - "file"
- Button Input - "button"
- Image Input - "image"
- Submit Input - "submit"
- Reset Input - "reset"
Comments
No Comments Have Been Made


