#native_company# #native_desc#
#native_cta#

FormHandling

By Sean
on September 11, 2002

Version: .5

Type: Class

Category: HTML

License: GNU General Public License

Description: This is a simple class for handling data from forms. At the moment, it only supports the input tag with the following attributes: “text” and “radio”. At the end of the file there is a short sample and description of how to use this class.

<?php

class FormHandling
{
  var $formStructure;
  var $strayFormVariables;
  var $errorFormVariables = array();
  var $passedFormVariables;
  var $postData;
  
  
  function FormHandling()
  {
    global $_POST;
    $this->postData = $_POST;
  }
  
  function ParseFormData()
  {
    reset( $this->formStructure );
    while( list( $key, $val ) = each( $this->formStructure ) )
    {
      switch( $val["type"] )
      {
        case "int":
          $this->CheckInt( $key, $val );
          break;
          
        case "string":
          $this->CheckString( $key, $val );
          break;
          
        case "radio":
          $this->CheckRadio( $key, $val );
          break;
          
        default:
      }
      
    }
    $this->StrayFormData();
  
  }
  
  
  function CheckInt( $formName, $variableStructure )
  {
    $this->postData["$formName"] += 0;
    if( !( $variableStructure["canBeEmpty"] ) && empty( $this->postData["$formName"] ) )
    {
      $this->errorFormVariables[] = $formName;
      return;
    } else if( !is_int( $this->postData["$formName"] ) )
    {
      $this->errorFormVariables[] = $formName;
      return;
    }
    
    $variableLength = strlen( $this->postData["$formName"] );
    if( $variableLength < $variableStructure["minLength"] &&
        $variableLength > $variableStructure["maxLength"] )
    {
      $this->errorFormVariables[] = $formName;
      return;
    } else
      $this->passedFormVariables[] = $formName;
      
    return;
  }
  
  function CheckString( $formName, $variableStructure )
  {
    if( !( $variableStructure["canBeEmpty"] ) && empty( $this->postData["$formName"] ) )
    {
      $this->errorFormVariables[] = $formName;
      return;
    } else if( !is_string( $this->postData["$formName"] ) )
    {
      $this->errorFormVariables[] = $formName;
      return;
    }
    
    $variableLength = strlen( $this->postData["$formName"] );
    
    if( $variableLength < $variableStructure["minLength"] &&
        $variableLength > $variableStructure["maxLength"] )
    {
      $this->errorFormVariables[] = $formName;
      return;
    } else
      $this->passedFormVariables[] = $formName;
      
    return;
  }
  
  function CheckRadio( $formName, $variableStructure )
  {
    if( !( $variableStructure["canBeEmpty"] ) && empty( $this->postData["$formName"] ) )
    {
      $this->errorFormVariables[] = $formName;
      return;
    }
    if( !in_array( $this->postData["$formName"], $variableStructure["possibleValues"] ) )
    {
      $this->errorFormVariables[] = $formName;
      return;
    }
    
    $this->passedFormVariables[] = $formName;
    return;
  }
  
  function StrayFormData()
  {
    reset( $this->postData );
    while( list( $key, $val ) = each( $this->postData ) )
    {
      if( empty( $this->formStructure["$key"] ) )
        $this->strayFormVariables[] = $key;
    }
  }
  
  function DisplayStructure( $dataArray = '' )
  {
    if( $dataArray == '' )
      $dataArray = $this->formStructure;

    reset( $dataArray );
    while( list( $key, $val ) = each( $dataArray ) )
    {
      echo $key;
      if( is_array( $val ) )
      {
        echo "<br />";
        $this->DisplayStructure( $val );
      } else
        echo " => ". $val ."<br />";
    }
  }
}