Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Web Design (WordPress, Joomla, etc.) Parse error: syntax error, unexpected T_ELSE

  • Parse error: syntax error, unexpected T_ELSE

    Posted by Travis Barr on January 1, 2011 at 1:24 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']) : '';

    ?>

    Travis Barr replied 15 years, 8 months ago 2 Members · 2 Replies
  • 2 Replies
  • Abraham Chaffin

    January 1, 2011 at 1:31 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

  • Travis Barr

    January 1, 2011 at 7:32 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.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy