FORUMS: list search recent posts

Parse error: syntax error, unexpected T_ELSE

COW Forums : Web Design

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Travis Barr
Parse error: syntax error, unexpected T_ELSE
on Jan 1, 2011 at 1:24:51 am

I have a script for a simple multi page form using sessions. I also have a simple script to add an inline error message on the same form. When I try to combine the two I am given a Parse error. It happens on:

else if ($stage == 2)

I think there is a lot here that I am messing up with, mostly, the placement of some functions. Any help is appreciated.

Here is the combined code:
<?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)
{

//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:
<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
}

// 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;
}
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
}

?>


Return to posts index

Abraham Chaffin
Re: Parse error: syntax error, unexpected T_ELSE
on Jan 1, 2011 at 1:31:24 am

You have an "else if" right after the end of a function the "}" before the else if is ending the function... that's why you have an error.

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;
}
else if ($stage == 2)
{


Abraham


Return to posts index

Travis Barr
Re: Parse error: syntax error, unexpected T_ELSE
on Jan 1, 2011 at 7:32:11 pm

Thank you, I moved around the function and it took care of the T_ELSE error, however were I put it just gave me more errors...oh boy. So I made a new thread. Thanks again for the help.


Return to posts index

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