Location: PHPKode > scripts > Form Maker and Processor > form-maker-and-processor/formMaker.class.php
<?php
/**************************************************/
/*
Released by AwesomePHP.com, under the GPL License, a
copy of it should be attached to the zip file, or
you can view it on http://AwesomePHP.com/gpl.txt
*/
/**************************************************/

/*
Form Maker Class - This class creates an XHTML form type document based
on inputs and parameters given by user. Please check implementation.php
for a sample implementation
*/

class createForm {
	
	var $formName;
	var $formTarget;
	var $formMethod;
	var $formAction;
	var $formEncypt;
	
	var $arrayHTML;
	var $arrayTEMP;
	
	var $hiddenHTML;
	
	/*
	Finalize form. We need this because we will not know if the forum requires encypt tag till
	the end
	*/
	function finalizeForm()
	{
		$this->arrayHTML = '<form name="'.$this->formName.'" id="'.$this->formName.'" target="'.$this->formTarget.'" method="'
			.$this->formMethod.'" '.$this->formEncypt.'action="'.$this->formAction.'">';		
		$this->makeTable();
		$this->arrayHTML .= $this->arrayTEMP;
		$this->endTable();
		$this->arrayHTML .= $this->hiddenHTML."\n</form>";
		unset($this->arrayTEMP);
	}
	
	/*
	Actually print the forum
	*/
	function printForm()
	{ 	
		echo $this->arrayHTML;
		unset($this->arrayHTML);
	}
	
	/*
	HTML functions - self explanatory
	*/
	function makeTable(){ 	$this->arrayHTML .= "\n<table>"; 	}
	function endTable(){ 	$this->arrayHTML .= "\n</table>";	}

	function makeTR(){ 		$this->arrayTEMP .= "\n<tr$rowspan>"; 		}
	function endTR(){ 		$this->arrayTEMP .= "\n</tr>";		}
	
	function makeTD($colspan = NULL,$rowspan = NULL){
		if($colspan != NULL){ $add = ' colspan="'.$colspan.'"';}
		if($rowspan != NULL){ $add .= ' rowspan"'.$rowspan.'"';}
					 		$this->arrayTEMP .= "\n\t<td$add>"; 	}
	function endTD(){ 		$this->arrayTEMP .= "</td>";		}
	
	/*
	Start the creation of the forum - Place form paramters in temp variables
	till we actually finalize the form.
	*/
	function doCreate($formName,$formTarget,$formMethod,$formAction)
	{
		$this->formName = $formName;
		$this->formTarget = $formTarget;
		$this->formMethod = $formMethod;
		$this->formAction = $formAction;				
	}
	
	/*
	Combines several HTML functions, avoid repetition
	*/
	function beforeObject($label){
		$this->makeTR();
		$this->makeTD();
		$this->arrayTEMP .= $label;
		$this->endTD();
		$this->makeTD();
	}
	
	function afterObject(){
		$this->endTD();
		$this->endTR();
	}
	
	/*
	Start group for checkboxes/radiobuttons
	*/
	function startGroup($label = NULL)
	{
		if($label != NULL){
			$this->beforeObject($label);
			return;
		}
		$this->makeTR();
		$this->makeTD();
	}
	
	function endGroup() {  	$this ->afterObject(); 				}
	
	/*
	Alert row, can contain any message
	*/
	function createAlert($label) {
		$this->makeTR();
		$this->makeTD(2);
		if(is_array($label)){
			foreach($label as $curLabel){ $this->arrayTEMP .= $curLabel."<br />"; }
		} else {
			$this->arrayTEMP .= $label;
		}
		$this->afterObject();
	}
	
	/*
	Create a textbox
	*/
	function createTextBox($label,$handler,$preset = NULL,$extras = NULL) {
		$this->beforeObject($label);
		$this->arrayTEMP .= '<input type="textbox" name="'.$handler.'" id="'.$handler.'"';
		if($preset != NULL){ $this->arrayTEMP .= ' value="'.$preset.'"'.$extras;}
		$this->arrayTEMP .= ' />';
		$this->afterObject();
	}
	
	/*
	Create a file upload and update form variables
	*/
	function createFileUpload($label,$handler,$preset = NULL,$extras = NULL){
		$this->beforeObject($label);
		$this->arrayTEMP .= '<input type="file" name="'.$handler.'" id="'.$handler.'"';
		if($preset != NULL){ $this->arrayTEMP .= ' value="'.$preset.'"'.$extras;}
		$this->arrayTEMP .= '>';
		$this->afterObject();
		if($this->formEncypt == NULL){ $this->formEncypt = 'enctype="multipart/form-data" ';}
	}
	
	/*
	Create text area
	*/
	function createTextArea($label,$handler,$preset = NULL,$extras = NULL) {
		$this->beforeObject($label);
		$this->arrayTEMP .= '<textarea name="'.$handler.'" id="'.$handler.'"'.$extras.'>'.$preset.'</textarea>';
		$this->afterObject();
	}
	
	/*
	Create checkbox
	*/
	function createCheckBox($label,$handler,$value = NULL,$preset = NULL, $checked = false, $extras = NULL){
		$this->arrayTEMP .= "\n\t\t".'<input type="checkbox" '.$extras.'name="'.$handler.'" id="'.$handler.'" value="'.$value.'"';
		if($checked || $preset == $value){ $this->arrayTEMP .= ' checked="checked"';}
		$this->arrayTEMP .= "'>$label\n\t";
	}
	
	/*
	Create radio button
	*/
	function createRadioButton($label,$handler,$value = NULL,$preset = NULL, $checked = false,$extras = NULL){
		$this->arrayTEMP .= "\n\t\t".'<input type="radio" '.$extras.'name="'.$handler.'" id="'.$handler.'" value="'.$value.'"';
		if($checked || $preset == $value){ $this->arrayTEMP .= ' checked="checked"';}
		$this->arrayTEMP .= "'>$label\n\t";
	}
	
	/*
	Create any button (should be private function)
	*/
	function createButton($handler,$preset = NULL,$type,$extras = NULL)
	{
		$this->makeTR();
		$this->makeTD(2);
		switch($type){
			case 1:
			$this->arrayTEMP .= '<input type="reset" name="'.$handler.'" id="'.$handler.'" value="'.$preset.'"'.$extras.'>';
			break;
			case 2:
			$this->arrayTEMP .= '<input type="button" name="'.$handler.'" id="'.$handler.'" value="'.$preset.'"'.$extras.'>';
			break;
			default:
			$this->arrayTEMP .= '<input type="submit" name="'.$handler.'" id="'.$handler.'" value="'.$preset.'"'.$extras.'>';
			break;
		}
		$this->afterObject();	
	}
	
	/*
	Make HTML buttons
	*/
	function makeReset($handler,$preset = NULL,$extras = NULL){ $this->createButton($handler,$preset,1,$extras);}
	function makeButton($handler,$preset = NULL,$extras = NULL){ $this->createButton($handler,$preset,2,$extras);}
	function makeSubmit($handler,$preset = NULL,$extras = NULL){ $this->createButton($handler,$preset,3,$extras);}
	
	/*
	Make a list menu
	*/
	function startListMenu($label,$handler,$extras = NULL)
	{
		$this->beforeObject($label);
		$this->arrayTEMP .= "\n\t\t".'<select name="'.$handler.'" id="'.$handler.'"'.$extras.'>'."\n";
	}
	
	/*
	Make a list menu with multiple selections
	*/
	function startListList($label,$handler,$size = 1,$is_multiple,$extras = NULL)
	{
		$this->beforeObject($label);
		$this->arrayTEMP .= "\n\t\t".'<select size="'.$size.'" '.$extras.'name="'.$handler.'" id="'.$handler.'"';
		if($is_multiple){ $this->arrayTEMP .= ' multiple';}
		$this->arrayTEMP .= '>'."\n";
	}
	
	/*
	End menu list
	*/
	function endList()
	{
		$this->arrayTEMP .= "\n\t\t</select>\n\t";
		$this->afterObject();	
	}
	
	/*
	Add options to list menus
	*/
	function addOption($label,$handler,$preset = NULL,$extras = NULL)
	{		
		$this->arrayTEMP .= "\n\t\t\t".'<option '.$extras.'value="'.$handler.'"';
		if($preset == $handler){ $this->arrayTEMP .= ' selected="selected"';}
		$this->arrayTEMP .= ">$label</option>\n";
	}
	
	/*
	Create image inputs
	*/
	function addImageInput($label,$handler,$src,$extras = NULL)
	{
		$this->beforeObject($label);
		$this->arrayTEMP .= '<input type="image" name="'.$handler.'" id="'.$handler.'" src="'.$src.'"'.$extras.'>';
		$this->afterObject();
	}
	
	/*
	Create hidden field
	*/
	function addHiddenInput($handler,$preset = NULL,$extras = NULL)
	{
		$this->hiddenHTML .= "\n".'<input type="hidden" name="'.$handler.'" id="'.$handler.'" value="'.$preset.'"'.$extras.'>';
	}
}

?>
Return current item: Form Maker and Processor