如何在提交表单时获取未选中的复选框列表?

时间:2010-06-02 16:42:50

标签: php

我有这个代码,例如:

$b = "";
 while ($row = mysql_fetch_array($rows)) {
        if ($row['enabled'] == 1) {
                    $b = "checked";
              } else {
                     $b = "":
                     }

echo“< \ input name ='nam [$ row [id]]'type ='checkbox'value ='$ row [id]'$ b />”;

}

当我执行此代码时,我将获得一个复选框列表,其中一些已经过检查,而另一些则没有。

我可以使用此代码获取已选中复选框的列表。

 if (isset($_POST['sub'])) { //check if form has been submitted or not
$nam = $_POST['nam'];
if (!empty($nam)) {

              foreach($nam as $k=>$val){

    // proccess operation with checked checkboxes
               }

}           

我需要知道如何在提交表单后获取未签名的复选框列表。

提前致谢。

2 个答案:

答案 0 :(得分:0)

浏览器不会为未选中的框发送任何内容,因此您唯一的希望是创建一个隐藏字段,告诉您每个复选框的名称(或为每个复选框添加隐藏字段)。

答案 1 :(得分:0)

如果动态创建复选框,则这是一项简单的任务:

创建命名约定。例如,假设每个复选框都绑定到DB中的一行,DB中的每一行都有一个主键。您可以根据此键为每个复选框命名:

<input type="checkbox" id="foo_1" name="foo_1" />
<input type="checkbox" id="foo_2" name="foo_2" />
<input type="checkbox" id="foo_3" name="foo_3" />

现在提交表单时,您可以查询数据库以获取Id并按如下方式处理请求:


$res = mysql_query("select * from foo;");
while($row = mysql_fetch_array($res))
    $checked = isset($_POST["foo_{$row['id']}");
    ...
}

相关问题