FORUMS: list search recent posts

Validating a multi page sessions form with php

COW Forums : Web Design

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Travis BarrValidating a multi page sessions form with php
by on Jan 1, 2011 at 7:30:21 pm

I have tried every way but the correct way to figure this one out. I have a multi page sessions form and I have an inline validation script. I am trying to figure out how to combine the two. For the sake of saving space I will not post all my mistakes, but bellow are the two scripts. Any advice or pointers would be welcomed, thanks.

Multi form script:

<?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
//Validation for the second stage
}
else if ($stage == 3)
{
?>
Hello <?php echo $_SESSION['first_name'] ?>
Your favorite color is <?php echo $_SESSION['color'] ?>
<?php
}

?>



################################
################################
################################

The validation script:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'GET')
{

//just display the form if the request is GET
display_form(array());
}
else
{
//the request is a POST so validate the form
$errors = validate_form();
if (count($errors))
{
//if there were any errors, redisplay the form with the errors
display_form($errors);
}
else
{
//the form data was valid so print a congratulations message
print 'the form was submitted.';
}
}

function display_form($errors)
{
//set up defaults
$defaults['first_name'] = isset($_POST['first_name']) ? htmlentities($_POST['first_name']) : '';

?>
<form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post">
First Name:
<?php print_error('first_name', $errors) ?>
<input type="text" name="first_name" />

<input type="submit" value="Say Hello" />
</form>
<?php }

// A helper function to make generating the HTML for an error message easier
function print_error($key, $errors) {
if (isset($errors[$key])) {
print "{$errors[$key]}";
}
}

function validate_form()
{
//start out with no errors
$errors = array();

//name is required
if (! strlen($_POST['first_name']))
{
$errors['first_name'] = 'Enter your first name.';
}
return $errors;
}
?>


Return to posts index

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