Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

PHP: form validation

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
grogoreo



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

PostPosted: Mon Dec 12, 2005 9:08 pm    Post subject: PHP: form validation Reply with quote

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:

<?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
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
CJLL
LXF regular


Joined: Sat Jul 09, 2005 10:22 pm
Posts: 193

PostPosted: Wed Dec 21, 2005 8:28 pm    Post subject: RE: PHP: form validation Reply with quote

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
Back to top
View user's profile Send private message
grogoreo



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

PostPosted: Fri Dec 23, 2005 2:10 pm    Post subject: RE: PHP: form validation Reply with quote

Wow thanks CJLL
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
A-Wing
LXF regular


Joined: Tue Jul 05, 2005 8:25 pm
Posts: 460
Location: Wellingborough

PostPosted: Tue Dec 27, 2005 2:04 pm    Post subject: RE: PHP: form validation Reply with quote

Although sessions aren't so wonderful once you hit a round robin cluster of servers Smile
_________________
Andrew Hutchings, Linux Jedi
www.a-wing.co.uk
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast