Location: PHPKode > scripts > SS Form > ss-form/ss_form.php
<?php
/* 
Simple Tableless Form Class
written by Richard Lainchbury - hide@address.com

This is a very simple class that enables you to distance hmtl code with content, 
It produces valid html to be used with simple css from a few basic values.
The main reason for using it is to unclutter the html written into code and it also makes it easy to edit forms and 
include in loops etc...
It doesnt validate user inputs or your own php, I'll leave that for you todo.

Please tell me if you like or dislike this class as its my first

Example Usage:

$contactForm = new SSform('enquiries_form','post',$_SERVER['REQUEST_URI'],'width: 500px;');
$contactForm->addInputText('email :','contact[email]');
$contactForm->addInputText('name :','contact[name]');
$contactForm->addInputPassword('password :','contact[password]');
$contactForm->addTextarea('address :','contact[address]');
$radio_options[] = array('Uk','select-value',1);
$radio_options[] = array('America','select-value2');
$contactForm->addInputRadio(' :',$radio_options,'contact[radioselect]');
$dropdown_options[] = array('option1','select-value');
$dropdown_options[] = array('option2','select-value2',1);
$contactForm->addDropDown('options :',$dropdown_options,'contact[dropdownselect]');
$contactForm->addRow('date :', date('jS F Y',time()));
$contactForm->addRow(NULL, 'random text or html');
$contactForm->addInputHidden('contact[number]', 'hidden text');
$contactForm->addSubmit('Submit');

$form = $contactForm->construct()

Special:
required custom functions-
function _style_item ( $text  , $tag = 'span', $class = 0 ){
	if($text){
	$code .= sprintf('<%s%s>%s</%s>',
				$tag,
				($class? ' class="'.$class.'"':false),
				$text,
				$tag);
	return ( $code );
	}
}
This is one of the many simple custom functions i use to make my life valid/easy.
	
CSS-
.form label {width: 100px; display: block; float: left;text-align:right}
.form {padding: 10px; margin: 0 15px;}
.form p {background-color: #aaa; border: solid 1px #DDEDFF; border-bottom-color: #D2E7FF; border-top-color: #E2F0FF; margin: 0; padding: 4px;}
.form input {width:200px}
.form textarea {width:300px}
.form input.submit{width:auto}
.form input.radio{width:auto}

replace .form with the form id to change specific forms
*/

function _style_item ( $text  , $tag = 'span', $class = 0 ){
	if($text){
	$code .= sprintf('<%s%s>%s</%s>',
				$tag,
				($class? " class=\"$class\"":false),
				$text,
				$tag);
	return ( $code );
	}
}

class SSform{
	var $form_id;
	var $form_action;
	var $form_method;
	var $form_style;
	var $row = array();
	
	
	function SSform($id=NULL,$method=NULL,$action=NULL,$style=NULL){
		$this->form_id = $id;
		$this->form_action = $action;
		$this->form_method = $method;
		$this->form_style = $style;
	}
	function addInputText($field, $name='', $value = NULL, $class = NULL){
		$data = sprintf('<input type="text" name="%s"%s%s />',
					$name,
					($class ? ' class="'.$class.'"' : false),
					($value ? ' value="'.$value.'"' : false) 
						);
		$this->row[] = array($field,$data);
	}
	function addInputFile($field, $name='', $value = NULL, $class = NULL){
		$data = sprintf('<input type="file" name="%s"%s%s />',
					$name,
					($class ? ' class="'.$class.'"' : false),
					($value ? ' value="'.$value.'"' : false) 
						);
		$this->row[] = array($field,$data);
	}
	function addInputPassword($field, $name='', $value = NULL, $class = NULL){
		$data = sprintf('<input type="password" name="%s"%s%s />',
					$name,
					($class ? ' class="'.$class.'"' : false),
					($value ? ' value="'.$value.'"' : false) 
						);
		$this->row[] = array($field,$data);
	}
	function addInputHidden($name='', $value = NULL){
		$data = sprintf('<input type="hidden" name="%s"%s />',
					$name,
					($value ? ' value="'.$value.'"' : false) 
						);
		$this->row[] = array(NULL,$data);
	}
	function addTextarea($field, $name='', $value = NULL ,$class=NULL){
		$data = sprintf('<textarea name="%s"%s rows="5" cols="20" >%s</textarea>',
					$name,
					($class ? ' class="'.$class.'"' : false),
					$value
						);
		$this->row[] = array($field,$data);
	}
	function addInputCheckbox($field, $name='',  $checked = NULL, $class = NULL){
		$data = sprintf('<input type="checkbox" name="%s"%s%s />',
					$name,
					($class ? ' class="'.$class.'"' : false),
					($checked ? ' checked="checked"' : false) 
						);
		$this->row[] = array($field,$data);
	}
	function addInputRadio($field, $options, $name=''){
			foreach($options as $optiondata){
					$data = sprintf('<input class="radio" type="radio" name="%s"%s%s /> '.$optiondata[0],
							$name,
							($optiondata[1] ? ' value="'.$optiondata[1].'"' : false),
							($optiondata[2] ? ' checked="checked"' : false)
								);
							
			$this->row[] = array($field,$data);
				$field = '&nbsp;';
		}
	}
	function addDropDown($field, $options, $name='', $class = NULL){
		$data = sprintf('<select name="%s"%s>',
					$name,
					($class ? ' class="'.$class.'"' : false));
		foreach($options as $optiondata)
			$data .= sprintf('<option%s%s>%s</option>',
				($optiondata[1] ? ' value="'.$optiondata[1].'"' : false),
				($optiondata[2] ? ' selected="selected"' : false),
				$optiondata[0]
			);
		$data .= '</select>';
		$this->row[] = array($field,$data);
	}	
	
	function addSubmit($value , $name='submit'){
		$data = sprintf('<input type="submit" class="submit" name="%s"%s />',
					$name,
					($value ? ' value="'.$value.'"' : false)
							);
		$this->row[] = array('&nbsp;',$data);
	}
	

	function addRow($field,$data){
		$this->row[] = array($field,$data);
	}
	
	function returnRow($field,$data){
		if($field)
			$code = _style_item(_style_item($field,'label').$data,'p');
		else
			$code = $data;
		return $code;
	}
	function construct(){
		$code .= sprintf('<form%s%s><div %s class="form"%s>',
							($this->form_method ? ' method="'.$this->form_method.'"' : false),
							($this->form_action? ' action="'.$this->form_action.'"' : false),
							($this->form_id ? 'id="'.$this->form_id.'"' : false),
							($this->form_style ? ' style="'.$this->form_style.'"' : false)
								);
	
		foreach($this->row as $rowdata)
			$code .= "\n\t\t\t".$this->returnRow($rowdata[0],$rowdata[1]);
			
		$code .= '</div>'."\n\t\t".'</form>';
		return $code;
	}
	
}?>
Return current item: SS Form