PHP: form validation

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

PHP: form validation

Postby grogoreo » Mon Dec 12, 2005 9:08 pm

hi

I have two questions, firstly, would you mind having a look at my code to see if I have used the code from a LinuxFormat tutorial (LXF65) correctly. The validation code should be fine, as I have just copied that, I mean how I have implemented the forms to connect with the validation. The second question is, how can I store the validated form fields from each form. I am sure an array is the best way, so could I create an array when the page is started then keep on adding to it with each form submit.

The reason I have different sets of forms is because I'm going to use this for an application form for the web site I'm making for my IT course. I need to validate form data. The fields in the form now are just temporary.

Here's the code:
Code: Select all
<?php
   // Check to see if $_POST is set
   if (isset($_POST)) {
      $errors = array();   
      foreach($_POST['form'] as $var => $val) {
         // split the varible name e.g. rs_name would be split into 'rs' and 'name'
         $name = explode("_", $var);
         if (count ($name) == 2) {
            // this has a prefix
            $parseblock = $name[0];
            $varname = $name[1];
            $parselen = strlen($parseblock);
            for ($i = 0; $i < $parselen; ++$i) {
               // loop through each letter in the prefix
               switch ($parseblock[$i]) {
                  case "r";
                     // field is required
                     if (!strlen($val)) {
                        $errors[] = "<strong>$varname</strong> is a required field";
                        break 2;
                     }
                     break;
                  case "i";
                     // field should be int
                     if (!is_numeric($val)) {
                        $errors[] = "<strong>$varname</strong> cannot be set to <em>$val</em>";
                        break 2;
                     }
                     break;
                  case "s";
                     // field should be string
                     if (!is_string($val)) {
                        $erros[] = "<strong>$varname</strong> cannot be set to <em>$val</em>";
                        break 2;
                     }
                     break;
                  }
               }
            } else {
               // this has no prefix
               $varname = $name[0];
            }
      }
         if (count($errors)) {
            // there were validation errors!
            foreach($errors as $val) {
               echo "$val<br />";
            }
         }
   }

   if ($_GET['stage'] == '2') {
      // Stage 2
      echo <<<FRMUT
      <form action="validation_form.php?stage=3" method="post">
      Comment: <input type="text" name="form[r_Comment]" /><br />
      
      <input type="hidden" name="stage_3">      

      <input type="submit" value="Go" />
      </form>
FRMUT;
   } else
   if ($_GET['stage'] == '3') {
      // Stage 3
      echo <<<FRMUT
      <form action="validation_form.php?stage=4" method="post">
      Age: <input type="text" name="form[ri_Age]" /><br />
      
      <input type="hidden" name="stage_4">      

      <input type="submit" value="Go" />
      </form>
FRMUT;
   } else
   if ($_GET['stage'] == '4') {
      echo "Your application is being processed<br />";
      echo "Thank you";
   } else {
      // Stage 1
      echo <<<FRMUT
      <form action="validation_form.php?stage=2" method="post">
      Name: <input type="text" name="form[rs_Name]" /><br />
      
      <input type="hidden" name="stage_2">
      
      <input type="submit" value="Go" />
      </form>
FRMUT;
   }
?>


Thanks,
Greg
grogoreo
 
Posts: 11
Joined: Tue Apr 12, 2005 6:46 pm
Location: www.grogoreo.co.uk

RE: PHP: form validation

Postby CJLL » Wed Dec 21, 2005 8:28 pm

Welcome to the wonderful world of sessions:

http://www.php.net/manual/en/ref.session.php

There are plenty of examples to help you get started
--
The reward for self love is sticky hands
CJLL
LXF regular
 
Posts: 193
Joined: Sat Jul 09, 2005 9:22 pm

RE: PHP: form validation

Postby grogoreo » Fri Dec 23, 2005 2:10 pm

Wow thanks CJLL
grogoreo
 
Posts: 11
Joined: Tue Apr 12, 2005 6:46 pm
Location: www.grogoreo.co.uk

RE: PHP: form validation

Postby A-Wing » Tue Dec 27, 2005 2:04 pm

Although sessions aren't so wonderful once you hit a round robin cluster of servers :)
Andrew Hutchings, Linux Jedi
http://www.a-wing.co.uk
User avatar
A-Wing
LXF regular
 
Posts: 460
Joined: Tue Jul 05, 2005 7:25 pm
Location: Wellingborough


Return to Programming

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 0 guests