Location: PHPKode > scripts > phpFormBuilder > php-form-builder-2010-02-01/Form.php
<?
/**
*
* @package luc
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @author Emily Brand
* 
* Minimum Requirement: PHP 5.2
*/

class Form
{
  public $values = array();  //Holds submitted form field values
  public $errors = array();  //Holds submitted form error messages
  public $num_errors;        //The number of errors in submitted form

  /* Class constructor */
  function Form(){
    /**
     * Get form value and error arrays, used when there
     * is an error with a user-submitted 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);
      $this->user = $_SESSION['username'];
    }
    else if(isset($_SESSION['value_array'])){
      $this->values = $_SESSION['value_array'];
      $this->user = $_SESSION['username'];
    }
    else if(isset($_SESSION['varray']) && isset($_SESSION['earray'])){
      $this->values = $_SESSION['varray'];
      $this->errors = $_SESSION['earray'];
      $this->num_errors = count($this->errors);
      $this->user = $_SESSION['username'];

      unset($_SESSION['varray']);
      unset($_SESSION['earray']);
    }
    else if(isset($_SESSION['varray'])){
      $this->values = $_SESSION['varray'];

      unset($_SESSION['varray']);
    }
    else if(isset($_SESSION['varray2'])){
      $this->values = $_SESSION['varray2'];

      unset($_SESSION['varray2']);
    }
    else{
      $this->num_errors = 0;
    }
  }

  /**
   * setValue - Records the value typed into the given
   * form field by the user.
   */
  function setValue($field, $value){
    $this->values[$field] = $value;
  }

  /**
   * setError - Records new form error given the form
   * field name and the error message attached to it.
   */
  function setError($field, $errmsg){
    $this->errors[$field] = $errmsg;
    $this->num_errors = count($this->errors);
  }

  /**
   * value - Returns the value attached to the given
   * field, if none exists, the empty string is returned.
   */
  function value($field){
    if(array_key_exists($field,$this->values)){
      return htmlspecialchars(stripslashes($this->values[$field]));
    }else{
      return "";
    }
  }

  /**
   * error - Returns the error message attached to the
   * given field, if none exists, the empty string is returned.
   */
  function error($field){
    if(array_key_exists($field,$this->errors)){
      return "<font color=\"#ff0000\">".$this->errors[$field]."</font>";
    }else{
      return "";
    }
  }

  /* getErrorArray - Returns the array of error messages */
  function getErrorArray(){
    return $this->errors;
  }

  /**
   * createTextBox - Returns a text box with the specified
   * parameters
   *
   * @param id the id & name for the textbox
   * @param value the value of the textbox
   * @param size default 20
   * @param maxsize default 50
   * @return String the html version of the textbox
   */
  function createTextBox($name, $id, $value, $size, $maxsize, $type = "VARCHAR"){
    return "<label for='$id'>$name</label><input type='text' id='$id' name='$id' ".
                "size='$size' maxlength='$maxsize' value='$value' />";
  }

  /**
   * createInputFile - Returns a input file box with the specified parameters
   *
   * @param id the id & name for the textbox
   * @param value the value of the textbox
   * @return String the html version of the textbox
   */
  function createInputFile($name, $id, $value, $type = "VARCHAR"){
    return "<label for='$id'>$name</label><input type='file' id='$id' name='$id' value='$value' />";
  }

  /**
   * createTextArea - Returns a textarea box with the specified parameters
   *
   * @param id the id & name for the textbox
   * @param value the value of the textbox default NULL
   * @param cols default 10
   * @param rows default 10
   * @return String the html version of the textbox
   */
  function createTextArea($name, $id, $value = NULL, $cols = 10, $rows = 10, $script = NULL){
    return "<label for='$id'>$name</label><textarea name='$id' id='$id' cols='$cols' rows='$rows' $script>$value</textarea>";
  }

  /**
   * createPasswordBox - Returns a password box with the specified parameters
   *
   * @param id the id & name for the textbox
   * @param value the value of the textbox
   * @param size default 20
   * @param maxsize default 50
   * @return String the html version of the textbox
   */
  function createPasswordBox($name, $id, $value, $size=20, $maxsize=50){
    return "<label for='$id'>$name</label><input type='password' id='$id' name='$id' ".
                "size='$size' maxlength='$maxsize' value='$value' />";
  }

  /**
   * createHidden - Returns a hidden box with the specified parameters
   *
   * @param id - the id & name for the textbox
   * @param value - the value of the textbox
   * @return - the html version of the hidden input box
   */
  function createHidden($id, $value){
    return "<input type='hidden' id='$id' name='$id' value='$value' />";
  }

  /**
   * createRadioButton - Returns a radio button with the specified parameters
   *
   * @param nameId - the id & name for the radio
   * @param value - the value of the radio button
   * @param checked - the value of the radio button
   * @param onchangeform - (optional) onchange command
   * @param formname - (optional) the formname for the onchange command
   * @return - the html version of the radio button
   */
  function createRadioButton($nameId, $id, $checkedValue, $type = "VARCHAR", $size = null){
    foreach($nameId as $name=>$value) {
      $c = ($checkedValue == $value) ? "checked='checked'" : "";
      $radio .= "<input type='radio' name='$id' id='$id' ".
                "value='$value' $c /><label for='$id'>$name</label>";
    }
    return $radio;
  }

  /**
   * createCheckBox - Returns a checkbox with the specified parameters
   *
   * @param id - the id & name for the checkbox
   * @param value - the value of the checkbox
   * @param checked - true or false
   * @return - the html version of the checkbox
   */
  function createCheckBox($name, $id, $value, $checked, $type = "VARCHAR", $size = null){
    foreach($nameId as $name=>$value) {
      $c = (in_array($value,$checkedValue)) ? "checked='checked'" : "";
      $check .= "<input type='checkbox' name='$id' id='$id' ".
                "value='$value' $c /><label for='$id'>$name</label>";
    }
    return $check;
  }

  /**
   * createSubmit - Returns a hidden box with the specified parameters
   *
   * @param id - the id & name for the submit button & hidden input box
   * @param value - the value of the submit button & hidden input box
   * @return - the html version of the hidden input box & the submit button
   */
  function createSubmit($id, $value){
    $c .= "<input type='submit' value='$value'>";
    return $c;
  }
   
  function text($text) {
    return $text;
  }
   
  function linebreak() {
    return "<br /><br />";
  }
  
  function finalizeForm($content) {
    return $content;
  }
};

?>
Return current item: phpFormBuilder