在POST变量- multiple复选框中也包括未选中的框


Include also unchecked boxes in the POST variable - multiple checkbox

我使用下面的代码在我的php文件中从多个复选框中获取值。

    if(!empty($_POST['check_list'])) {
        foreach($_POST['check_list'] as $check) {
                update_comment_meta($check, 'consider', 1);
        }

    }

问题是,这段代码显然是把数组$_POST['check_list']只检查值。

我的需要是通过将'0'作为第三个参数而不是'1'来对未检查的值执行update_comment_meta函数。

关于更多细节,我给出了生成HTML表单的代码:
<form action="" id="primaryPostForm" method="POST">
<?php    
         $defaults = array(
    'post_id' => $current_post); 
         $com= get_comments( $defaults );
        foreach ($com as $co) {
    if(get_comment_meta($co->comment_ID, 'consider', true)==1) {
    ?><input type="checkbox" name="check_list[]" value="<?php echo $co->comment_ID; ?>" checked="checked">
    <?php }
    else {
    ?><input type="checkbox" name="check_list[]" value="<?php echo $co->comment_ID; ?>" >
    <?php
    }}
</form>

发送未检查的值到post有点不那么容易。更好的解决方案是你命名复选框的方式,使用它,你可以很容易地在post页面迭代它们。

使用隐藏输入和复选框。复选框优先于隐藏输入

<form>
  <input type='hidden' value='0' name='check_box_con'>
  <input type='checkbox' value='1' name='check_box_con'>
</form>

现在提交后,由于两者具有相同的名称,如果未选中check_box_con将显示隐藏字段值,否则将覆盖并显示原始值

查看更多信息张贴未选中的复选框

这是我使用的解决方案(基于PeeHaa评论):

        if(!empty($_POST['check_list'])) {
        foreach ($com as $co) {
        if (in_array($co->comment_ID,$_POST['check_list']))
        update_comment_meta($co->comment_ID, 'consider', 1);
        else 
         update_comment_meta($co->comment_ID, 'consider', 0);
        }
        }

事实上,POST变量是这样与复选框一起工作的,所以简单的方法是使用服务器端语言来知道哪些值不是通过POST发送的。

谢谢你的时间。