即使未选中,也会发布复选框值。 (复选框的名称是数组格式)

时间:2016-03-10 10:33:30

标签: php jquery html checkbox

我目前无法发布尚未检查过的复选框,需要您的帮助。以下是我目前使用的代码。

PHP

while($select_info2 = mysqli_fetch_assoc($select_info1)){

$n='1';
echo "<tr>";
    echo "<td>".$select_info2['Device_DNS_name']."</td>";
    echo "<td>".$select_info2['Uplink_port']."</td>";
    echo "<td>".$select_info2['VLAN']."</td>";
    echo "<td>".$select_info2['Teamed_network']."</td>";
    echo "<td>".$select_info2['Teamed_description']."</td>";
    for($a = $select_detailed_dns2['starting_port']; $a <= $select_detailed_dns2['ending_port']; $a++ ){
        if('10' > $a){
            if ($select_info2["port-0".$a] == "1" || $select_info2["port-0".$a] == "01" ){
                $check ='checked="checked"';
            }else{
                $check = '';
            }
            ?>
                <td><input type="checkbox" name="check_<?php echo $select_info2['ID']."[]"?>" id="check_<?php echo $select_info2['ID']?>"  <?php echo $check ?> value = "On" /></td> 
            <?php
        }else{
            if ($select_info2["port-".$a] == "1" || $select_info2["port-".$a] == "1" ){
                $check ='checked="checked"';
            }else{
                $check = '';
            }
            ?>
                <td><input type="checkbox" name="check_<?php echo $select_info2['ID']."[]"?>" id="check_<?php echo $select_info2['ID']?>"  <?php echo $check ?> value = "On" /></td>
            <?php
        }
    }
    ?>
    <td><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.checktable.check_<?php echo $select_info2['ID']?>)"> </td>
    <td><input type="button" name="Un_Check_All" value="Uncheck All" onClick="UnCheckAll(document.checktable.check_<?php echo $select_info2['ID']?>)"> </td>
    <td><input type="submit" name="update_row" value="Update Row" > </td>
    <?php
echo "</tr>";   

}

上面的代码生成复选框,具体取决于数据库中表中有多少列。您可能会问为什么名称是数组格式。这样我就可以发布所有值。您可能也会问为什么id是相同的而不是不同的。这样我就可以定位整行来检查或取消选中。以下是我目前输出所有选中复选框的方式:

<?php
if(isset($_POST['update_row'])){
    $c1 = $_POST['check_1'];
    var_dump($c1);
}

&GT;

生成的HTML

<td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                    <td><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.checktable.check_1)"> </td>
                            <td><input type="button" name="Un_Check_All" value="Uncheck All" onClick="UnCheckAll(document.checktable.check_1)"> </td>
                            <td><input type="submit" name="update_row" value="Update Row" > </td>
                            </tr><tr><td>HEPSAN03</td><td>No</td><td>NET44</td><td>No</td><td></td>                                         <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                    <td><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.checktable.check_2)"> </td>
                            <td><input type="button" name="Un_Check_All" value="Uncheck All" onClick="UnCheckAll(document.checktable.check_2)"> </td>
                            <td><input type="submit" name="update_row" value="Update Row" > </td>
                            </tr><tr><td>HEPSAN03</td><td>No</td><td>PRIV44</td><td>No</td><td></td>                                            <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                    <td><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.checktable.check_4)"> </td>
                            <td><input type="button" name="Un_Check_All" value="Uncheck All" onClick="UnCheckAll(document.checktable.check_4)"> </td>
                            <td><input type="submit" name="update_row" value="Update Row" > </td>
                            </tr><tr><td>HEPSAN03</td><td>No</td><td>NET40</td><td>No</td><td></td>                                         <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                    <td><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.checktable.check_5)"> </td>
                            <td><input type="button" name="Un_Check_All" value="Uncheck All" onClick="UnCheckAll(document.checktable.check_5)"> </td>
                            <td><input type="submit" name="update_row" value="Update Row" > </td>
                            </tr>

希望你能帮忙 谢谢

1 个答案:

答案 0 :(得分:0)

你能不设置default =“0”或“2”或其他东西并检查它?

也许这有助于... Post the checkboxes that are unchecked