PHP数组复选框问题,保持'正确'提交后检查框

时间:2016-08-29 20:34:04

标签: javascript php jquery arrays checkbox

我目前创建了一个基于数组中每个学校的复选框系统。一切正常,但提交后,只检查第一个复选框。 示例:我有10个复选框,复选框号为4,5和6.提交后,选中框1,2和3。 我希望方框4,5和6在提交后保留其选择。 希望你可以通过问题来想象。

<!--Start Checkbox system for schools-->
<form method="POST">
<?php
$q = "SELECT id, name FROM $school_table";  
$result = mysqli_query($con, $q);
    while(($row =  mysqli_fetch_array($result))) {
    //First line of <input>
    echo '<input type="checkbox" name="check_list[]" value="';  

    //Value of the input (school ID)
    echo $row['id'] .'"';                                       

    //Keep the box checked after submit. PROBLEM MIGHT BE HERE!!!
    if(isset($_POST['check_list'][$row['id']])){echo 'checked="checked"';} 

    //Echos the school name out after the checkbox. 
    echo '>' . $row['name'] . " <br> ";                         
    }
?>
<script language="javascript">
//Select all on/off function
function checkAll(bx) {
    var cbs = document.getElementsByTagName('input');
    for(var i=0; i < cbs.length; i++) {
            if(cbs[i].type == 'checkbox') {
            cbs[i].checked = bx.checked;
            }
    }
}
</script>
<!--Check all mark. Works with Javascript written above-->
<input type="checkbox" name="check_all" onclick="checkAll(this)" <?php if(isset($_POST['check_all'])){echo "checked";} ?>> Check all: On/Off
<input type="submit" name="submit" value="sort"> 
</form>
<!--End Checkbox system for schools-->

Image of the problem. Take a look

希望你们能帮助我:)

1 个答案:

答案 0 :(得分:0)

我没有你的MySQL,所以我通过使用for循环创建一些行来模拟它。

<html>
<!--Start Checkbox system for schools-->
<head>
<title>Test checklist</title>
</head>
<body>
<form method="POST">
<?php
     for ($i = 0; $i < 10; $i++) {
         $rows[$i]['id'] = $i*2+1;
         $rows[$i]['name'] = "Name ".strval($i*2+1);
     }
 //     $q = "SELECT id, name FROM $school_table";
 //$result = mysqli_query($con, $q);
 //while(($row =  mysqli_fetch_array($result))) {
     foreach ( $rows as $row )
     {
    //First line of <input>
    echo '<input type="checkbox" name="check_list['.$row['id'].']" value="checked"';

    //Keep the box checked after submit. PROBLEM MIGHT BE HERE!!!
    if(isset($_POST['check_list'][strval($row['id'])])){
        echo 'checked="checked"';}

    //Echos the school name out after the checkbox.
    echo '>' . $row['name'] . " <br>\n";
}
?>
<script language="javascript">
//Select all on/off function
    function checkAll(bx)
{

    var cbs = document.getElementsByTagName('input');
    for(var i=0; i < cbs.length; i++) {

        if(cbs[i].type == 'checkbox') {

            cbs[i].checked = bx.checked;
        }
    }
}
</script>
<!--Check all mark. Works with Javascript written above-->
<input type="checkbox" name="check_all" onclick="checkAll(this)" <?php if(isset($_POST['check_all'])){
    echo "checked";} ?>> Check all: On/Off
<input type="submit" name="submit" value="sort">
</form>
<!--End Checkbox system for schools-->
</body>
</html>

关键是这一行:

echo '<input type="checkbox" name="check_list['.$row['id'].']" value="checked"';

您需要将check_list中的数组索引设置为您的行ID。否则 数组中的行将基于0并具有值=您的行ID。您对帖子字段的价值实际上并没有按照您编码的方式使用。

check_list[0] = '1'
check_list[1] = '3'
check_list[2] = '5'