簡體   English   中英

PHP:帶有POST的分步表單

[英]PHP: Step-by-step forms with POST

如何使用$_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和重要的

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

您的復選框名稱必須是oc而不是“ optional-checkbox”,因為這里$oc = @$_POST['oc']; ,您正在尋找名稱為oc輸入。

錯誤2

<?php
    }
    elseif {

您的elseif沒有條件。 只能是else並且您在elseif{之后立即錯過了一個?>

錯誤3

</form>

    }
}

在這里,您錯過了<?php </form>}之間的<?php標簽

工作守則

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM