PHP Classes
elePHPant
Icontem

File: example3.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of M H Rasel  >  Form Validation  >  example3.php  >  Download  
File: example3.php
Role: Example script
Content type: text/plain
Description: Radio & Check box Example
Class: Form Validation
Validate submitted form values according to rules
Author: By
Last change:
Date: 5 years ago
Size: 4,076 bytes
 

Contents

Class file image Download
<?php
/**
  * -------------------------------------------------------------------------
  * PHP Form Validator (formvalidator.php)
  * Version 1.0
  * -------------------------------------------------------------------------
  * @author M H Rasel
  * 4-5-2009::0:15
  * @copyright 2009
  * email:mail@monjur-ul-hasan.info
  * Documentation: http://phpformvalidation.monjur-ul-hasan.info
 */


 
require "class.validator.inc";
 
// if form is posted
 
if($_POST['action']=="Check")
 {
   
    
//// if form is posted
   
$data = $_POST; // collect the data from the form
   
   
$rules = array(
   
   
"language"=>array(
       
"require"=>true, // the user need to u click any of the radio
       
"type"=>"radio" // in the case of radio button you have add this line inside the rules
       
),
   
"continental"=>"require" // user need to select any of the item
   
);

   
$message = array(
   
"language"=>array(
       
"require"=>"Choose One language" // if user dont select any radio button then it will now work
       
),
   
"continental"=>"Select a continental" // if user dont select any item then this will shown
   
);

// create the instance
$objValidator = new Validator($rules,$message);

echo
"<h2><a href='#'>Validator Result </a></h2>";
// call the validation proce
if(!$objValidator->isValid($data))
{
   
// error found
   
    // get the list of form fields which have errorful data
   
$errors = $objValidator->ErrorFields();
   
// show the number of error
   
echo "<br/> you have total ".$objValidator->CountError()." errors.<br/>Fields are :".implode(",",$errors);
    echo
"<br/><hr/>";
   
    foreach(
$errors as $key=>$var)
    {
        echo
"For the field <b>$var</b> errors are: ";
       
// collect error for each error full field
       
$value = $objValidator->getErrors($var);
       
        if(
is_array($value))
           
// if the field have more then one validation rule faild then an array will return
           
echo implode(" * ",$value);
        else echo
$value;
        echo
"<br/>";
    }
    
}
else echo
"valid";
echo
"<br/><br/>";

}
?>


<form method="POST" action="">
    <table border="1" width="100%">
        <tr>
            <td>Language</td>
            <td><input type="radio" value="Bangla" id="language" <?php echo ($data["language"]=="Bangla")?"checked ":""?> name="language">Bangla
            <input type="radio" value="English" id="language" <?php echo ($data["language"]=="English")?"checked ":""?> name="language">English
            <input type="radio" value="Others" id="language" <?php echo ($data["language"]=="Others")?"checked ":""?> name="language">Others</td>
        </tr>
        <tr>
            <td>Continental </td>
            <td><select name="continental">
            <option selected value="" >--- Select One ---</option> <!-- this special. by default this item need to be select to work the form validator --!>
            <option <?php echo ($data["continental"]=="Asia")?"selected ":""?> value="Asia">Asia</option>
            <option value="Europe" <?php echo ($data["continental"]=="Europe")?"selected ":""?>>Europe</option>
            <option value="Africa" <?php echo ($data["continental"]=="Africa")?"selected ":""?>>Africa</option>
            <option value="Australia" <?php echo ($data["continental"]=="Australia")?"selected ":""?>>Australia</option>
            <option value="Antertica" <?php echo ($data["continental"]=="Antertica")?"selected ":""?>>Antertica</option>
            <option value="North America" <?php echo ($data["continental"]=="North America")?"selected ":""?>>North America</option>
            <option value="South America" <?php echo ($data["continental"]=="South America")?"selected ":""?>>South America</option>
            </select></td>
        </tr>
        </table>
    <p><input type="submit" value="Check" name="action"><input type="reset" value="Reset" name="B2"></p>
</form>
  </div>