user6247920
user6247920

Reputation:

PHP: Step-by-step forms with POST

How can I make "step-by-step" screens with $_POST method, with optional levels inside? this is my code:

<?php
    $next = @$_POST['next'];
    $prev = @$_POST['prev'];

    if( !isset($next) || isset($prev) ) {
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="checkbox" name="optional-checkbox" id="oc" />
    <label for="oc">If you'll check me, you'll see an optional level</label>
    <button name="prev" type="submit">Previous</button>
    <button name="next" type="submit">Next</button>
</form>

<?php
    }
    else {
        // if isset next
        $oc = @$_POST['oc'];
        if( isset($oc) ) {
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    You've checked the optional checkbox. now you can continue, or - go back.
    <button name="prev" type="submit">Previous</button>
    <button name="next" type="submit">Next</button>
</form>

<?php
        }
        elseif {

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php 
        // check if we came from the "oc" level, if true - show special message, else - just continue
        if( $oc == true ) {
            echo 'You\'ve checked the optional checkbox. now you\'re on the 3rd level.';
        }
        else {
            echo 'now you\'re on the 3rd level.';
        }
    ?>
    <button name="prev" type="submit">Previous</button>
    <button name="next" type="submit">Next</button>
</form>

        }
    }

When I test it, its not showing levels properly and I can't continue from the optional level to the next level. I sure did something wrong, but I don't know what... also, I can't go back - if I'll unset $_POST['next'], it will send me to the first level, from every level. any suggestions/ thoughts?

Thank you all.

Upvotes: 0

Views: 181

Answers (1)

jonju
jonju

Reputation: 2736

First of all your code has some syntax errors.

Error 1 and the Important one

<input type="checkbox" name="optional-checkbox" id="oc" />

Name of your checkbox must be oc not 'optional-checkbox' because here $oc = @$_POST['oc'];, you are looking for input with name oc.

Error 2

<?php
    }
    elseif {

Your elseif has no condition. it should be else only and you missed a ?> immediately after the elseif{.

Error 3

</form>

    }
}

Here you missed <?php tag between </form> and }

Working Code

<?php
    $next = @$_POST['next'];
    $prev = @$_POST['prev'];
    if( !isset($next) || isset($prev) ) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="checkbox" name="oc" id="oc" />
    <label for="oc">If you'll check me, you'll see an optional level</label>
    <button name="prev" type="submit">Previous</button>
    <button name="next" type="submit">Next</button>
</form>
<?php
    }
    else {
    // if isset next
       $oc = @$_POST['oc'];
        if( isset($oc) ) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    You've checked the optional checkbox. now you can continue, or - go back.
    <button name="prev" type="submit">Previous</button>
    <button name="next" type="submit">Next</button>
</form>
<?php
    }
    else{
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php 
        // check if we came from the "oc" level, if true - show special message, else - just continue
        if( $oc == true ) {
           echo 'You\'ve checked the optional checkbox. now you\'re on the 3rd level.';
        }
        else {
            echo 'now you\'re on the 3rd level.';
        }
    ?>
    <button name="prev" type="submit">Previous</button>
    <button name="next" type="submit">Next</button>
</form>
<?php
        }
    }
?>

Upvotes: 1

Related Questions