Location: PHPKode > scripts > Get form value & error > get-form-value-error/form.class.php
<?php
/**
* @package Form modul class
* @author $Author: Lukas Mestan $
* @version $Id: form.php,v 1.0 2008/09/29 15:58:15 Lukas Mestan Exp $
* @since v.1.5.3
* @copyright (c) Lukas Mestan
*/
class Form{
   /**
    * @var array $values
    */
   var $values = array();
   /**
    * @var array $errors
    */
   var $errors = array();
   /**
    * @var int $num_errors
    */
   var $num_errors;
   /**
    * Class construct
    */
   function Form(){
      if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){
         $this->values = $_SESSION['value_array'];
         $this->errors = $_SESSION['error_array'];
         $this->num_errors = count($this->errors);
         unset($_SESSION['value_array']);
         unset($_SESSION['error_array']);
      }else{
         $this->num_errors = 0;
      }
   }
   /**
    * Set form value
    * @param string $field
    * @param string $value
    * @return string
    */
   function setValue($field, $value){
      $this->values[$field] = $value;
   }
   /**
    * Set error messages for form
    * @param string $field
    * @param string $value
    * @return string
    */
   function setError($field, $errmsg){
      $this->errors[$field] = $errmsg;
      $this->num_errors = count($this->errors);
   }
   /**
    * Get value from form
    * @param string $field
    * @return string
    */
   function value($field){
      if(array_key_exists($field,$this->values)){
         return htmlspecialchars(stripslashes($this->values[$field]));
      }else{
         return "";
      }
   }
   /**
    * Write / show error mesager for form
    * @param string $field
    * @return string
    */
   function error($field){
	  global $locale;
      if(array_key_exists($field,$this->errors)){
         return "<span style='color:#ff0000;font-size:10px;'>".$this->errors[$field]."</span>";
      }else{
         return "";
      }
   }
   /**
    * Get all count error array
    * @return array
    */
   function getErrorArray(){
      return $this->errors;
   }
};
?>
Return current item: Get form value & error