FORUMS: list search recent posts

How to validate a session() multi page form using php?

COW Forums : Web Design

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Travis BarrHow to validate a session() multi page form using php?
by on Jan 31, 2011 at 7:52:26 am

I would like to make sure that there are no empty fields. I would like to add

if (! strlen($_POST['first_name']))
{
print 'Please enter your first name.';
}

but I am not sure where to add it. any help would be appreciated.

Thanks.

<?php

//Turn on sessions
session_start();

//Find what stage to use
if (($_SERVER['REQUEST_METHOD'] == 'GET') || (!isset($_POST['stage'])))
{
$stage = 1;

}
else
{
$stage = (int) $_POST['stage'];
}

// Save any submitted data
if ($stage > 1)
{
foreach ($_POST as $key => $value)
{
$_SESSION[$key] = $value;
}
}
if ($stage == 1)
{
?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>'

First Name:
<input type='text' name='first_name' />

<input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
<input type='submit' name='submit' value='Next' />
</form>
<?php
}
else if ($stage == 2)
{
?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>

Favorite Color:
<input type='text' name='color' />

<input type='hidden' name='stage' value='<?php echo $stage +1 ?>'/>
<input type='submit' value='Done'/>
</form>
<?php
}
else if ($stage == 3)
{
?>
Hello <?php echo $_SESSION['first_name'] ?>
Your favorite color is <?php echo $_SESSION['color'] ?>
<?php

}
?>


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]