Location: PHPKode > scripts > Form Maker > form-maker/formmaker.class.php
<?php
/*--------------------------------------------------------------
FORM MAKER - by Juliano Lopes Millidiu - 2005/03
----------------------------------------------------------------
This class let you create web forms in a simple way, just call
the methods and pass the correct parameters. 
You can set other details, like css, tables properties, 
all to make your form default with your web site or application.

Here we have an example to how implement the class, making a simple
form:

$FORM = new formMaker();
$FORM->setTable("width='100%' cellpadding='2' cellspacing='0' border='0'");
$FORM->setForm("empresas","POST",LOCAL_PATH."?on=".$_GET[on]."&sub=".$_GET[sub]."&in=".$nextIn."");
$FORM->setColsProperty("width='30%' align='right'");
$FORM->setCssInput("verdana_10_666666");
$FORM->setCssLabel("verdana_10_666666");
$FORM->makeTextField("empresa_razao_social_r","Razão Social:","40");
$FORM->makeTextField("empresa_fantasia_r","Fantasia:","40");
$FORM->makeTextField("empresa_cnpj_r","CNPJ:","20","onKeyDown=\"FormataCNPJ(this,event)\"");
$FORM->makeTextField("empresa_ie","Inscriçao Estadual:","20");
$FORM->makeTextField("empresa_im","Inscrição Municipal:","20");
//you can make templates to the form and put on like this (sections)
$FORM->makeSection("sectionForm.tpl", "<b>EMPRESAS VISUALIZADAS</b>");	
$FORM->makeCheckSqlBox("fieldname","field label","select key,field from table ,5);
$FORM->makeImageField("btn_incluir.gif","btn_gravar","align='center'","onClick=\"this.form.submit();\"");	

and that's it. check out the other methods to see the other ways to make your form

--------------------------------------------------------------*/
class formMaker {
	
	var $output;
	var $labelcss;
	var $inputcss;
	var $script;
	var $sql;
	var $tdlabelsize;
	var $tdinputsize;
	var $tablesize;
	var $DB;
	var $enc;

	function formMaker($enctype=FALSE,$php2go=FALSE){	
		
		if(!$php2go){
 			$this->DB = Connect_DB();
		}else{
			
			$this->DB =& Db::getInstance();			
		
		}
 		if($enctype) $this->enc = "ENCTYPE='multipart/form-data'";
 	}
 	
 	function setDB ($db_host,$db_user,$db_pass,$db_name,$db_used) {
 		$this->DB = Connect_DB($db_host,$db_user,$db_pass,$db_name,$db_used);
 	}
 	
 	function setCssLabel($css){
  		$this->labelcss = $css;
 	}
 	
 	function setCssInput($css){
 		$this->inputcss = $css;
 	}
 	
 	function  setTable($attrs){
    	$this->output = "<table ".$attrs." width='".$this->tablesize."'>\n";
 	}
 	
 	function setForm($name,$method="POST",$action='".$_PHP_SELF."'){
    	$this->output .= "<form action='".$action."' method='".$method."' name='".$name."' ".$this->enc.">\n";
 	}
 	
 	function setColsProperty($prop){
 		$this->tdProps = $prop;
 	}
 	
 	function makeButtonField($name,$label){
 		$this->output .="<tr>\n";
		$this->output .="	<td colspan='2' ".$attrs."><input type='button' value='".$label."' name='".$name."' script='".$script."'></td>\n";
		$this->output .="</tr>\n";
 	}
 	
 	function makeHiddenField($name,$value){
		$this->output .="<input type='hidden' value='".$value."' name='".$name."'>\n";	
 	}
 	
 	function makeCheckSqlBox($name,$label,$sql,$quebra){
 	 
 	 	 $RS=$this->DB->Execute($sql);
	  	 $this->output .="<tr>\n";
	  	 $this->output .="<td ".$this->tdProps." class='".$this->labelcss."'>".$label."</td>\n";
	  	 $this->output .="<td class='".$this->labelcss."'>";
	 	 $this->output .="<table>";
	  	 $this->output .=" <tr>";
	  	 $c=1;
	  	 while($regs = $RS->FetchRow()){
		    $id=$regs[0];
	  	  	if(!empty($GLOBALS[$name][$id])){
			    $ck = "checked='true'";
	  	 	}else{
	  	 		$ck="";
	  	 	}
	  	    $this->output .="<td valign='top' class='".$this->labelcss."'><input type='checkbox' ".$ck." name='".$name."[]' value='".$regs[0]."'>".$regs[1]."</td>\n";
	   		if($c==$quebra){
	   		   $this->output.="</tr><tr>\n";
	  	 	   $c=0;
	  	 	}
	  	 	$c++;
	  	 }  	
	  	 $this->output .="</tr>";
	  	 $this->output .="	</table>"; 
	  	 $this->output .=" </td>\n";
	  	 $this->output .="</tr>\n";
 	}
 	
 	function makeRadioField($name,$label,$vals,$script=""){
		$this->output .="<tr>\n";
	 	$this->output .="<td ".$this->tdProps." class='".$this->labelcss."'>".$label."</td>\n";
		$this->output .="<td  class='".$this->labelcss."'>\n";
		$rvals = explode("|",$vals);
		foreach($rvals as $key=>$value){
			$d = explode("#",$value);
			$ck="";
			
			if($GLOBALS[$name]==$d[1]){
			   $ck = "checked";
			}
			$this->output .= "<input type='radio' ".$ck." name='".$name."' value='".$d[1]."' ".$script.">".$d[0]."\n";
		}
		$this->output .="</td>\n";
	    $this->output .="</tr>\n";  		   	
 	}
 	
 	function makeTextArea($name,$label,$rows,$cols){
		$GLOBALS[$name] = stripslashes($GLOBALS[$name]);
	 	$this->output .="<tr>\n";	
		$this->output .="	<td ".$this->tdProps." class='".$this->labelcss."'>".$label."</td>\n";
	   	$this->output .="	<td width='".$this->tdinputsize."'><textarea name='".$name."'  class='".$this->inputcss."' rows='".$rows."' cols='".$cols."'>".$GLOBALS[$name]."</textarea></td>\n";
	   	$this->output .="</tr>\n";
 	}
 	
 	function makeTextField($name,$label,$size,$script,$maxlength="100",$labelinput){
	 	$this->output .="<tr>\n";
		$this->output .=	"<td ".$this->tdProps." class='".$this->labelcss."' width='".$this->tdlabelsize."'>".$label."</td>\n";
	  	$this->output .=	"<td width='".$this->tdinputsize."' class='".$this->labelcss."'><input size='".$size."' maxlength='".$maxlength."' type='textfield' name='".$name."' value='".$GLOBALS[$name]."' class='".$this->inputcss."' ".$script."> $labelinput</td>\n";
	  	$this->output .="</tr>\n";
 	}
 	
 	function makeOnlyText($value,$label){
	 	$this->output .="<tr>\n";
		$this->output .=	"<td ".$this->tdProps." class='".$this->labelcss."' width='".$this->tdlabelsize."'>".$label."</td>\n";
	  	$this->output .=	"<td width='".$this->tdinputsize."' class='".$this->labelcss."'><div align=\"justify\">$value</div></td>\n";
	  	$this->output .="</tr>\n";
 	}
 	
 	function makeSelectField($name,$label,$sql="",$script="",$options="",$obs){
	 	$this->output .="<tr>\n";
	 	
	 	if(!empty($sql)){
			
	  		$RS=$this->DB->Execute($sql);
	   		$this->output .="<td  class='".$this->labelcss."' ".$this->tdProps.">".$label."</td>\n";
	   		$this->output .="<td class='".$this->labelcss."'>".$obs."<select name='".$name."' class='".$this->inputcss."' ".$script." >\n";
	   		$this->output .="<option value='-1'>Selecione</option>\n";
	   		while($regs = $RS->FetchRow()){
	   			
	   			if($regs[0] == $GLOBALS[$name]){
	   				$sel = "selected";
	   			}else{
	   				$sel = "";
	   			}
	   			$this->output .="<option  value='".$regs[0]."' ".$sel.">".$regs[1]."</option>\n";
	  		}
	 	}else if(empty($options)){
	 		$this->output .="<td class='".$this->labelcss."' ".$this->tdProps.">".$label."</td>\n";
	   		$this->output .="<td  ".$this->labelcss.">".$obs."<select name='".$name."' class='".$this->inputcss."'>\n";
	   		$this->output .="<option value='-1'>Selecione</option>\n";
	 	}else{
	 		$this->output .="<td  class='".$this->labelcss."' ".$this->tdProps.">".$label."</td>\n";
	 		$this->output .="<td class='".$this->labelcss."'>".$obs."<select name='".$name."' class='".$this->inputcss."' ".$script.">\n";
	 		$o = explode("#",$options);
	 		foreach($o as $value){
	 
	 			$ov = explode("|",$value);
	 			$this->output .="<option value='".$ov[1]."'>".$ov[0]."</option>\n";
	 		}
	 	}
	 	$this->output .="</td>\n";
		$this->output .="</select>\n";
	 	$this->output .="</tr>\n";	
 	}
 	
 	function makePasswordField($name,$label,$size){
	 	$this->output .="<tr>\n";
	 	$this->output .="	<td ".$this->tdProps." class='".$this->labelcss."'>".$label."</td>\n";
	  	$this->output .="	<td><input type='password' name='".$name."' size='".$size."' value='' class='".$this->inputcss."'></td>\n";
	  	$this->output .="</tr>\n";
	}
	
	function makeFileField($name,$label,$size){
	 	$this->output .="<tr>\n";
	 	$this->output .="	<td ".$this->tdProps." class='".$this->labelcss."'>".$label."</td>\n";
	  	$this->output .="	<td><input type='file' name='".$name."' size='".$size."' value='' class='".$this->inputcss."'></td>\n";
	  	$this->output .="</tr>\n";
	}
	
	function makeImageField($img_name,$name,$tdattrs="",$script="",$style=""){
	 	$this->output .="<tr>\n";
	 	$this->output .=" <td colspan='2' ".$tdattrs."><input type='image'  name='".$name."'  src='".IMAGE_PATH."".$img_name."'' ".$script."  $style></td>\n";
	 	$this->output .="</tr>\n";
	}
	
	function makeOnlyImage($img_name,$tdattrs,$link){
	 	if($link != "") {
	 		$a1 = "<a href='$link'>";
	 		$a2 = "</a>";	
	 	}
	 	$this->output .="<tr>\n";
	 	$this->output .=" <td colspan='2' ".$tdattrs.">$a1 <img src='".IMAGE_PATH."".$img_name."'' border='0'> $a2</td>\n";
	 	$this->output .="</tr>\n";
	}

	function makeSection($tpl,$label="Section name"){
	 	 if(empty($tpl)){
	 	 	 echo "erro: template deve ser setado!";
	 	 	 exit;
	 	 } else{
	 	 	  $thtml = file_get_contents(TEMPLATE_PATH.$tpl);	
	 	 	  $final = str_replace("{labelSection}",$label,$thtml);
	 		  $this->output .= $final;
	 	 }	
	}
	
	function generate(){
	 	$this->output .=" </form>\n";
	 	$this->output .="</table>\n";
	
	 	return $this->output;
 	}
}

?>
Return current item: Form Maker