PHP数组复选框问题,请在提交后选中“正确”框

时间:2022-09-25 15:00:27

I am currently created a checkbox system based on each school in an array. Everything works fine except, that after submit, only the first checkboxes are checked. Example: I have 10 checkboxes, and check box number 4,5 and 6. After submit, box 1,2 and 3 are checked. I want box 4,5 and 6 to keep their selection after submit. Hope that you can imagine by problem.

我目前创建了一个基于阵列中每个学校的复选框系统。一切正常,但提交后,只检查第一个复选框。示例:我有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

问题的形象。看一看

Hope you guys can help me out :)

希望你们能帮助我:)

1 个解决方案

#1


0  

I didn't have your MySQL, so I simulated it with by creating some rows using a for loop.

我没有你的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>

The key line is this one:

关键是这一个:

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

You need to set the array index in check_list to your row id. Otherwise the rows in the array are going to be 0 based and have values = your row id. Your value for the post field really isn't used the way you are coding it.

您需要将check_list中的数组索引设置为您的行ID。否则,数组中的行将基于0并具有值=您的行ID。您对post字段的值实际上并没有像编码那样使用。

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

#1


0  

I didn't have your MySQL, so I simulated it with by creating some rows using a for loop.

我没有你的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>

The key line is this one:

关键是这一个:

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

You need to set the array index in check_list to your row id. Otherwise the rows in the array are going to be 0 based and have values = your row id. Your value for the post field really isn't used the way you are coding it.

您需要将check_list中的数组索引设置为您的行ID。否则,数组中的行将基于0并具有值=您的行ID。您对post字段的值实际上并没有像编码那样使用。

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