我已经试了几天来想办法解决这个问题,但是在测试场景的过程中无法正确解决。基本上,这是在一个编辑表单上,所以它从数据库中提取存储的值。我有它保存为“1”或“0”,1正在检查。它也是一个复选框,而不是数组。
我正试图找出如何使会话正确地存储选中的复选框,以便如果页面上有错误等,它会正确地显示我的会话值,但发生的情况是,DB值通常最终会胜过这个值。
所以,我如何首先显示DB值,然后显示会话值(如果更改)?
这是我一直在玩的东西。

if(isset($_SESSION["include_due_date"])) {
        $include_due_date = 'checked="checked"';
} else {
    if((!isset($_SESSION["include_due_date"])) && $resInvoice[0]["include_due_date"]  == 1 ) {
            $include_due_date = 'checked="checked"';
    } elseif((isset($_SESSION["include_due_date"])) && $resInvoice[0]["include_due_date"]  == 0 ) {
            $include_due_date = 'checked="checked"';
    } elseif((!isset($_SESSION["include_due_date"])) && $resInvoice[0]["include_due_date"]  == 0 ) {
            $include_due_date = '';
    }
}

有什么想法吗?
附加方法:
<input type="checkbox" value="1" <?php echo (isset($_SESSION['include_due_date'])?' checked="checked"' : (!isset($_SESSION['include_due_date'])) && $resInvoice[0]['include_due_date']  == 1?' checked="checked"' : ''); ?> name="include_due_date" id="include_due_date" />

最佳答案

所以,我如何首先显示DB值,然后再显示
会话值是否更改?

session_start(); // (Be sure to use session_start)
// If the include_due_date was set by the session
if(isset($_SESSION["include_due_date"])) {
    // Set the value of $checked to the value of the session variable
    // In this way, the value of the session variable takes precedence over that in the database
    $checked = ($_SESSION["include_due_date"] === true);
} else {
    // The session variable wasn't set, so use the value from the database to set both the checked value and the session variable.
    // The next request will use the value from the session variable
    if($resInvoice[0]["include_due_date"] == 1 ) {
        $_SESSION["include_due_date"] = $checked = true;
    } else {
        $_SESSION["include_due_date"] = $checked = false;
    }
}

HTML格式
<input type="checkbox" value="1" <?php if ($checked) echo 'checked="checked"; ?> name="include_due_date" id="include_due_date" />

10-07 19:20
查看更多