Location: PHPKode > scripts > Form creater > form-creater/classes/form.class.php
<?php
/**
 * Form class
 *
 * <p>This form can be used to generate forms</p>
 * <p><b>Usage:</b></p>
 	  $oSmarty = new Smarty;
 	  $aForm['start'] = Form::start('form.html','post','form');
		$aForm['username'] = Form::textfield('username','frank');
		$aOptions = array('1'=>'Number 1','2'=>'Number 2','3'=>'Number 3 is selected','4'=>'Number 4');
		$sSelected = '3';
		$aForm['select'] = Form::select('select',$aOptions,true,$sSelected);
		$aForm['radio'] = Form::radio('radio',$aOptions,$sSelected);
		$aForm['reset'] = Form::button('reset','reset','reset');
		$aForm['submit'] = Form::button('submit','submit','send!');
		$aForm['stop'] = Form::stop();
		$oSmarty->assign('Form',$aForm);
		$oSmarty->display('form.tpl');
 * <p><b>Result:</b></p>
 * <form action='form.html' method='post' name='form' id='form'>
 * <input type='text' name='username' value='frank'>
 * <input class='' type='submit' name='submit' value='send!'>
 * </form>
 * 
 * @author Frank Broersen
 * @version 0.1
 */
class Form {

	/**
	 * public static function start();
	 * 
	 * Creates the start of the form
	 * 
	 * @param $p_sTarget index.html
	 * @param $p_sMethod post / get
	 * @param $p_sName form
	 * @return string
	 */
  public static function start($p_sTarget = '',$p_sMethod = 'post',$p_sName = 'form') {
  	return "<form action='".$p_sTarget."' method='".$p_sMethod."' name='".$p_sName."' id='".$p_sName."'>";  	
  }  

	/**
	 * public static function stop();
	 * 
	 * Creates the end of the form
	 * 
	 * @return string
	 */
  public static function stop() {
  	return "</form>";  	
  }
  
  /**
	 * public static function button();
	 * 
	 * Creates a button
	 * 
	 * @return string
	 */
	public static function button($p_sType = 'submit',$p_sName = 'submit',$p_sValue = 'submit',$p_sClass = '') {
  	return "<input class='".$p_sClass."' type='".$p_sType."' name='".$p_sName."' value='".$p_sValue."'>";  	
  }
  
  /**
	 * public static function textarea();
	 * 
	 * Creates a textarea
	 * 
	 * @param $p_sName textarea name
	 * @param $p_sValue default value
	 * @param $p_sCols number of characters
	 * @param $p_sRows number of lines
	 * @param $p_sClass a css class
	 * @return string
	 */	
  public static function textarea($p_sName,$p_sCols,$p_sRows,$p_sValue = '',$p_sClass = '') {
  	return "<textarea class='".$p_sClass."' name='".$p_sName."' cols='".$p_sCols."' rows='".$p_sRows."'>".$p_sValue."</textarea>";
  }
  
  /**
	 * public static function textfield();
	 * 
	 * Creates a textfield
	 * 
	 * @param $p_sName textfield name
	 * @param $p_sValue default value
	 * @param $p_sClass a css class
	 * @return string
	 */	
  public static function textfield($p_sName,$p_sValue = '',$p_sClass = '') {
  	return "<input type='text' name='".$p_sName."' class='".$p_sClass."' value='".$p_sValue."'>";
  }
  
  /**
	 * public static function select();
	 * 
	 * Creates a select dropdown
	 * 
	 * @param $p_sName dropdown name
	 * @param $p_aOptions array with options
	 * @param $p_nEmpty leave one field empty at the beginning of the form
	 * @param $p_sSelected the selected value
	 * @param $p_sClass a css class
	 * @return string
	 */	
  public static function select($p_sName,$p_aOptions,$p_nEmpty = false,$p_sSelected = '',$p_sClass = '') {
  	$sHTML = "<select name='".$p_sName."' class='".$p_sClass."'>";
  	if($p_nEmpty) {
  		$sHTML .= "<option value=''></option>";
  	}
  	foreach($p_aOptions as $sName => $sValue) {
  		if($p_sSelected == $sName) {
  			$sHTML .= "<option value='".$sName."' selected='yes'>".$sValue."</option>";
  		} else {
  			$sHTML .= "<option value='".$sName."'>".$sValue."</option>";
  		}
  	}
  	$sHTML .= "</select>";
  	return $sHTML;
  }
  
  /**
	 * public static function radio();
	 * 
	 * Creates an array with radio buttons
	 * 
	 * @param $p_sName radio name
	 * @param $p_aOptions array with options
	 * @param $p_sSelected the selected value
	 * @param $p_sClass a css class
	 * @return array
	 */	
  public static function radio($p_sName,$p_aOptions,$p_sSelected = '',$p_sClass = '') {
  	$aHTML = array();
  	foreach($p_aOptions as $sName => $sValue) {
  		if($p_sSelected == $sName) {
  			$aHTML[$sName] = "<input type='radio' name='".$p_sName." class='".$p_sClass."' value='".$sName."' checked='yes'>".$sValue;
  		} else {
  			$aHTML[$sName] = "<input type='radio' name='".$p_sName." class='".$p_sClass."' value='".$sName."'>".$sValue;
  		}
  	}
  	return $aHTML;
  }
    
}
?>
Return current item: Form creater