记住具有初始默认值的表单复选框状态

时间:2013-01-24 21:23:49

标签: php html forms post

我有一个带复选框的表单。当用户首次访问该页面时,我希望“选中”复选框。但是,如果他们取消选中该框然后提交该页面,我希望它保持未选中状态(如果他们提交了已检查过的页面,则保持检查状态。)

要确定何时检查并提交表单,我现在正在做:

<input type='checkbox' class='seeAll' name='seeAll' value='true' <?php if ($_POST['seeAll'] == 'true') echo checked; ?>>

这非常适合在需要时检查框,但是,如果他们以这种方式提交,我将如何确保它保持未选中状态,同时还要检查他们是否重新访问该页面(例如重新输入URL )?

谢谢!

3 个答案:

答案 0 :(得分:1)

我不知道为什么花了这么长时间才得出这个答案,但在努力解决这个问题之后,我意识到我可以通过$_POST检查复选框的值,就像我在做的那样在之前,可以检查用户是否通过提交按钮以外的其他方式到达页面:

<?php if(($_POST['seeAll'] == 'true') || !isset($_POST['submit'])) echo checked; ?>

如果用户提交了表单,则isset($_POST['submit'])将为真,因此如果情况 $_POST['seeAll']为空,则显然用户提交了未选中的复选框。如果isset($_POST['submit'])为false,则用户在未提交表单的情况下到达页面,我应将复选框选为“默认”。

那么我的整个<input>标签看起来像这样:

<input type='checkbox' class='seeAll' name='seeAll' value='true' <?php if(($_POST['seeAll'] == 'true') || !isset($_POST['submit'])) echo checked; ?>>

就像我需要的那样工作!

答案 1 :(得分:0)

注意::这与OP的问题不同,因为它会记住复选框的值,即使用户离开页面(例如,到www.facebook.com)然后再回到页面。 OP希望它只记住页面发布时复选框的值。

如果您想要非永久性方法,可以使用$ _SESSION:

<?php
    if (!isset($_SESSION)) {
        session_start();
    }   

    if ($_POST['seeAll'] == 'true' || !isset($_SESSION['seeAll'])) {
        $_SESSION['seeAll'] = true;
    } else {
        $_SESSION['seeAll'] = false;
    }
?>


<form method="POST">
    <input type='checkbox' class='seeAll' name='seeAll' value='true' 
        <?php if ($_SESSION['seeAll']) echo 'checked'; ?>
    />
    <input type='submit'/>
</form>

请参阅:http://php.net/manual/en/session.examples.basic.php

答案 2 :(得分:0)

一种解决方案是同时使用$_SESSION[""]$_POST[""]。 (见下面的解释和代码

说明

  1. 如果未设置$_SESSION["loadcount"],请将其设置为0.
  2. 每个if (isset($_POST["submit"],将$_SESSION["load count"]增加+1。
  3. 在输入type =“checkbox”的'checked'选项中,回显PHP。如果if ($_SESSION["loadcount"] == 0)回显'已检查'。 (将复选框设置为最初检查的状态。)
  4. 否则,如果(isset($_POST["agree"] echo'选中',则记住用户检查的状态。 (记住用户设置的检查状态。
  5. CODE

    <?php
    session_start();
    ?>
    <!DOCTYPE html>
    <html>
        <body>
            <?php  // session_firstload.php
    
                // Check if $_SESSION is not set, set $_SESSION loadcount to 0
                if (!isset($_SESSION["loadcount"])) 
                    $_SESSION["loadcount"] = 0;
    
                // When the user submits using POST Method
                if (isset($_POST["submit"]))
                {
                    // Increase $_SESSION["loadcount"] by +1
                    $_SESSION["loadcount"] = $_SESSION["loadcount"] + 1;
                }
    
                // Echoing for Debugging / Understanding
                echo $_SESSION["loadcount"];
            ?>
    
            <form action="session_firstload.php" method="post">
                <input id="agree" type="checkbox" name="agree" 
                <?php 
                    // If Check the Checkbox is $_SESSION["loadcount"] == 0
                    // Else 'remember' with (isset($_POST["agree"]
                    if ($_SESSION["loadcount"] == 0) 
                            echo "checked";
                        else
                        {
                            if (isset($_POST["agree"]))
                                echo "checked";
                        }
                ?>
                >I Agree<br>
                <input type="submit" name="submit">
            </form>
        </body>
    </html>
    
相关问题