| View previous topic :: View next topic |
| Author |
Message |
grogoreo
Joined: Tue Apr 12, 2005 7:46 pm Posts: 11 Location: www.grogoreo.co.uk
|
Posted: Mon Dec 12, 2005 9:08 pm Post subject: PHP: form validation |
|
|
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 |
|
 |
CJLL LXF regular
Joined: Sat Jul 09, 2005 10:22 pm Posts: 193
|
Posted: Wed Dec 21, 2005 8:28 pm Post subject: RE: PHP: form validation |
|
|
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 |
|
 |
grogoreo
Joined: Tue Apr 12, 2005 7:46 pm Posts: 11 Location: www.grogoreo.co.uk
|
Posted: Fri Dec 23, 2005 2:10 pm Post subject: RE: PHP: form validation |
|
|
| Wow thanks CJLL |
|
| Back to top |
|
 |
A-Wing LXF regular

Joined: Tue Jul 05, 2005 8:25 pm Posts: 460 Location: Wellingborough
|
Posted: Tue Dec 27, 2005 2:04 pm Post subject: RE: PHP: form validation |
|
|
Although sessions aren't so wonderful once you hit a round robin cluster of servers  _________________ Andrew Hutchings, Linux Jedi
www.a-wing.co.uk |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|