PHP:POST的逐步表单

时间:2016-07-31 19:40:08

标签: php

如何使用$_POST方法制作“逐步”屏幕,内置可选级别?这是我的代码:

<?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>

        }
    }

当我测试它时,它没有正确显示水平,我无法从可选级别继续到下一级别。我确实做错了什么,但我不知道...还有,我不能回去 - 如果我要取消$_POST['next'],它会把我送到第一级,从每个级别。有什么建议/想法吗?

谢谢大家。

1 个答案:

答案 0 :(得分:1)

首先,您的代码存在一些语法错误。

错误1和重要错误

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

您的复选框的名称必须为oc而不是&#39;可选 - 复选框&#39;因为此处$oc = @$_POST['oc'];,您正在寻找名为oc的输入。

错误2

<?php
    }
    elseif {

您的elseif没有条件。它应该只是else而你在?>之后错过了elseif{

错误3

</form>

    }
}

您错过了<?php</form>

之间的}标记

工作代码

<?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
        }
    }
?>