如何计算while循环中未选中的复选框

时间:2011-10-24 09:02:47

标签: php mysql

$query = mysql_query("SELECT DISTINCT status, vendor FROM tbl_softwareinstalled WHERE vendor NOT LIKE ''");
$nums = mysql_num_rows($query);
echo "<form name = 'filter' action='softwarefilter.php' method='POST'>
$nums<br>

<table border = 1><tr><td>&nbsp;</td>
<td><strong>Software Vendor</strong></td></tr>";

$ctr1 = 1;
while($fetch = mysql_fetch_array($query)) {
$vendor = $fetch['vendor'];
$status = $fetch['status'];
    if(($ctr1 % 2)==1)
        { print "<tr bgcolor = 'white'>";}
        else 
        { print "<tr bgcolor = '#EEEEEE'>"; }
    print "<td><input name= 'chk[]' type='hidden' value='0'>
    <input type = 'checkbox' name = 'chk[]' value = '$vendor' ";




    if ($status == 'Enabled') {
    print "checked = 'checked'></td><td>$vendor</td></tr>"; }
    else {
    print "></td><td>$vendor</td></tr>"; }
    $ctr1++;
}
print "</table>";
print "<input type = 'submit' name = 'submit' value = 'Update Filter'>
</form> ";


$submit = $_POST['submit'];
if(isset($submit))
{ 
    $chk = $_POST['chk'];
    $count = count($chk);

    if (empty($chk)) {

    echo "qweqwe<br>";
    }

        for ($i=0; $i<$count; $i++) {
        $abc = $chk[$i];
        $query = mysql_query("UPDATE tbl_softwareinstalled SET status = 'Enabled' WHERE vendor = '$abc'");

        }

    echo '<meta http-equiv="refresh" content="0.5;url=/assets/softwarefilter.php">
            <script language="javascript">
            alert("Software filter updated.");
            </script>';
}

嗨,如何使用此代码块在while循环中计算UNCHECKED CHECKBOXES? 比方说,例如,我有10个带10个值的复选框,用户在提交后检查了3个复选框(值为'abc','def','ghi')。它将统计所有UNCHECKED复选框并回显它的值。 3个复选框的值不应该被回显。

2 个答案:

答案 0 :(得分:0)

浏览器只会告诉您哪些已经已检查。从数据库中拉出所有可能的框,然后删除浏览器告诉您的那些框。

答案 1 :(得分:0)

我曾经计算过类似的东西来计算未经检查的盒子。 放置输入类型复选框的两个输入字段和隐藏不同名称的输入类型

<input type="hidden" class="" name="test[]" value="0">
<input type="checkbox" class="" name="checkbox[]" value="1">

然后提交

if (isset($_POST['submit'])) {
    $cnt = array();
    $counter =0;
    $testcnt = 0;
    $cnt = 0;
    if (isset($_POST['test'])) {
        $testcnt = count($_POST['test']);
    }
    if (isset($_POST['checkbox'])) {
        $cnt = count($_POST['checkbox']);
    }
    echo 'the count of unchecked boxes is ';
    print_r($testcnt-$cnt);
}

根据您的情况,将其更改为:

<input name= 'unchecked[]' type='hidden' value='0'>

然后在您检查isset(提交)时提交,您可以相应地添加以下内容并进行相应更改

$unchecked_input = count($_POST['unchecked']);
$chk = $_POST['chk'];
$count = count($chk);
echo $unchecked_input-$count; //will output the count of unchecked boxes