Location: PHPKode > scripts > Simple Form > simple-form/SimpleForm.php
<?php
/**
 * Class SimpleForm
 * @author Mihai Bojin, September 2008
 * @version 1.00
 * @copyright GPL
 *
 */
class SimpleForm 
{
	private $formName;
	private $method;
	private $successMessage;
	private $elements;
	private $extraElements;
	
	private $values;
	private $error;
	
	private $formData;
	
	private $mailSubject;
	private $mailHeader;
	
	public function __construct($formName = 'SimpleForm', $method='post')
	{
		$this->formName = $formName;
		$this->method = $method;
		$this->addSuccessMessage('');
		
		$this->elements = array();
		$this->extraElements = array('submitted'=>1, 'referer'=>2 );
		
		$this->values = array();
		$this->error = array();
		
		$this->initMail();
	}
	
	public function initMail($htmlEmail = true) 
	{
		if ( $htmlEmail === true) {
			$this->mailHeader = "\nContent-Type: text/html; charset=ISO-8859-2\nMIME-Version: 1.0";
		}
		else {
			$this->mailHeader = '';
		}
	}

	public function addSuccessMessage($message)
	{
		$this->successMessage = $message;
	}
	
	public function addElement($label, $type='input-text', $required=false, $errorMessage='')
	{
		$name = $this->cleanString($label);
		$this->elements[$name] = array('name'=>$name, 'label'=>$label, 'type'=>$type, 'error'=>$errorMessage);
	}
	
	public function renderForm()
	{
		$this->loadValues();

		$string = '';
		$thisPageHref = $this->getThisPageHref();
		$timeStamp = gmmktime();
		$string .= <<<RTT
<form name="{$this->formName}" id="{$this->formName}" method="$this->method" action="{$thisPageHref}">
<fieldset>
<input type="hidden" name="{$this->formName}_submitted" value="{$timeStamp}" />
<input type="hidden" name="{$this->formName}_referer" value="{$thisPageHref}" />
RTT;
		foreach ($this->elements as $name=>$element) {
			$elValue = htmlentities($this->loadVar($this->values[$name], ''));
			switch ($element['type']) {
				case 'input-text':
					$string.= <<<RTT
<label for="{$this->formName}_{$name}">{$element['label']}</label><input type="text" name="{$this->formName}_{$name}" id="{$this->formName}_{$name}" value="{$elValue}" />
RTT;
				break;
				case 'input-password':
					$string.= <<<RTT
<label for="{$this->formName}_{$name}">{$element['label']}</label><input type="password" name="{$this->formName}_{$name}" id="{$this->formName}_{$name}" value="{$elValue}" />
RTT;
				break;
				case 'textarea':
					$string.= <<<RTT
<label for="{$this->formName}_{$name}">{$element['label']}</label><textarea name="{$this->formName}_{$name}" id="{$this->formName}_{$name}" rows="" cols="">{$elValue}</textarea>
RTT;
				break;
				case 'input-submit':
					$string.= <<<RTT
<input type="submit" name="{$this->formName}_{$name}" id="{$this->formName}_{$name}" value="{$element['label']}" />
RTT;
			}
		}
		$string .= <<<RTT
</fieldset>
</form>
RTT;
		return $string;
	}
	
	public function runForm($returnValues=false)
	{
		$renderedForm = $this->renderForm();
		$returnString = '';
	
		if (!$this->values['submitted']) {
			if (!empty($this->error)) {
				$errString = implode('<br />', $this->error);
				$returnString .= <<<RTT
<span class="error">$errString</span>
RTT;
			}
			$returnString .= $renderedForm;
			if ($returnValues === false) {
				echo $returnString;
				return true;
			}
			else {
				return $returnString;
			}
		}
		else {
			
			if (!empty($this->error)) {
				
				$errString = implode('<br />', $this->error);
				$returnString .= <<<RTT
<span class="error">$errString</span>
RTT;
				$returnString .= $renderedForm;
				if ($returnValues === false) {
					echo $returnString;
					return true;
				}
				else {
					return $returnString;
				}
			}
			else {
				$formData = '';
				$formValues = array_diff_key( $this->values, $this->extraElements );
				foreach ($formValues as $name=>$value) {
					
					if ($this->elements[$name]['type']!=='input-submit') {
						$formData .= <<<RTT
{$this->elements[$name]['label']}: $value<br />

RTT;
					}
				}
				
				$this->formData = $formData;
				if ( !$this->successMessage ) {
					return true;
				}
				else if ($returnValues === false) {
					echo $this->successMessage;
					return true;
				}
				else {
					return $this->successMessage;
				}
			}
		}
	}
	
	public function mailTo($to, $from, $subject) 
	{
		if ($this->formData) {
			$emailTo = $this->loadVar($this->values[$this->cleanString($to)], $to);
			$emailFrom = $this->loadVar($this->values[$this->cleanString($from)], $from);
			$emailBody = $this->formData;
			if (strlen($this->mailHeader)==0) {
				$emailBody = str_replace('<br />', '', $emailBody);
			}
			else {
				$emailBody = strip_tags($emailBody, '<br>');
			}
			$emailTo = $this->cleanEmail($emailTo);
			$emailFrom = $this->cleanEmail($emailFrom);
			@mail($emailTo, $subject, $emailBody, "From: {$emailFrom}{$this->mailHeader}" );
		}
	}
	
	protected function loadValues()
	{
		$values = array();
		if ($this->method == 'post') {
			foreach ($this->elements as $name=>$element) {
				$value = $this->loadVar($_POST[$this->formName. '_'. $name]);
				if ($value) {
					$values[$name] = $value;
				}
				else {
					$error[$name] = $element['error'];
				}
			}
			foreach ($this->extraElements as $name=>$e) {
				$values[$name] = $this->loadVar($_POST[$this->formName. '_'. $name], '');
			}
		}
		else if ($this->method == 'get') {
			foreach ($this->elements as $name=>$element) {
				$value = $this->loadVar($_GET[$this->formName. '_'. $name]);
				if ($value) {
					$values[$name] = $value;
				}
				else {
					$error[$name] = $element['error'];
				}				
			}
			foreach ($this->extraElements as $name=>$e) {
				$values[$name] = $this->loadVar($_POST[$this->formName. '_'. $name], '');
			}
		}
		$this->values = $values;
		$this->error = $error;
	}

	protected function cleanString($string)
	{
		return strtolower(preg_replace('/[^a-zA-Z]+/', '-', $string));
	}
	
	protected function cleanEmail($string)
	{
		return preg_replace('/[^a-zA-Z0-9-_.@]+/', '', $string);
	}
	
	protected function getThisPageHref()
	{
		return $_SERVER['REQUEST_URI'];;
	}
	
	protected function loadVar(&$value, $defaultValue=null)
	{
		if (isset($value)) {
			return $value;
		}
		else {
			return $defaultValue;
		}
	}

}
?>
Return current item: Simple Form