简体   繁体   English

如何将复选框数组中的数据保存到数据库中

[英]How do I save data from a checkbox array into database

 function check_uncheck(truefalse) { var boxes = document.forms[0].chkboxarray.length; var form = document.getElementById('checkForm'); for (var i = 0; i < boxes; i++) { if (truefalse) { form.chkboxarray[i].checked = true; } else { form.chkboxarray[i].checked = false; } } } 
 <form name="checkForm" id="checkForm" method="post" action="checkboxes1.php"> <input type="checkbox" name="chkboxarray" value="1" /><br /> <input type="checkbox" name="chkboxarray" value="2" /><br /> <input type="button" name="CheckAll" value="Check All Boxes" onclick="check_uncheck(true)" /> <input type="button" name="UncheckAll" value="Uncheck All Boxes" onclick="check_uncheck(false)" /> <input type="submit" value="Save"> </form> The snippet shows how it works without connection to a database 

I am trying to save the data that is sent from the checklist to a database but i'm stuck. 我试图保存从清单发送到数据库的数据,但是我被卡住了。 I was thinking of using a foreach but I don't know what to put in it. 我当时在考虑使用foreach,但是我不知道该放什么。

I though of putting it as: 我虽然把它作为:

foreach($_POST['id'] as $add){
insert into database...
}

How do I do this? 我该怎么做呢?

If I do this as Fred-ii and xjstratedgebx suggested where I just change name="chkboxarray" to name="chkboxarray[]" then the javascript code would stop working. 如果按照Fred-ii和xjstratedgebx的建议进行操作,那么我只是将name="chkboxarray" to name="chkboxarray[]"更改name="chkboxarray" to name="chkboxarray[]"那么javascript代码将停止工作。

<?php
include '../conec.php';
mysql_select_db("test",$conec)or die('Database does not exist.') or die(mysql_error());

$sql = mysql_query("SELECT * FROM user WHERE state='Not Signed Up'");
?>

<form name="checkForm" id="checkForm" method="post" action="checkboxes1.php">
    <?php
    while($row = mysql_fetch_array($sql)){
        $id = $row['id'];
        $name = $row['name'];
        $lName= $row['lName'];
        $concate = $name.' '.$lName;
        echo '<input type="checkbox" name="chkboxarray" value="'.$id.'" />'.$concate.'<br />';
    }


    ?>
    <!--<input type="checkbox" name="chkboxarray" value="1" /><br />
    <input type="checkbox" name="chkboxarray" value="2" /><br />-->
    <input type="button" name="CheckAll" value="Check All Boxes" onclick="check_uncheck(true)" />
    <input type="button" name="UncheckAll" value="Uncheck All Boxes" onclick="check_uncheck(false)" />
    <input type="submit" value="Save">

</form>

<script type="application/javascript">
function check_uncheck(truefalse){
    var boxes = document.forms[0].chkboxarray.length;
    var form = document.getElementById('checkForm');
    for(var i=0;i < boxes;i++){
        if (truefalse) {
            form.chkboxarray[i].checked=true;
        } else {
            form.chkboxarray[i].checked=false;
        }
    }
}
</script>

If you change the name of your checkbox from "chkboxarray" to "chkboxarray[]", then any boxes that are checked when the form is submitted will pass their values as an array to the server under the key of "chkboxarray". 如果将复选框的名称从“ chkboxarray”更改为“ chkboxarray []”,则在提交表单时选中的所有框都将其值作为数组传递给“ chkboxarray”键下的服务器。

Basically, change this line: 基本上,更改此行:

echo '<input type="checkbox" name="chkboxarray" value="'.$id.'" />'.$concate.'<br />';

To: 至:

echo '<input type="checkbox" name="chkboxarray[]" value="'.$id.'" />'.$concate.'<br />';

As a result, if you var_dump the $_POST super global, you should see something like: 结果,如果您使用var_dump $_POST超全局变量,则应该看到类似以下内容:

array(1) {
    [chkboxarray]=>
    array(2) {
        [0]=>
        string(1) "3"
        [1]=>
        string(1) "4"
    }
}

In the example above, the checkboxes for id's 3 and 4 were checked, so they were sent to the server. 在上面的示例中,选中了ID为3和4的复选框,因此将其发送到服务器。

Once you have that array, inserting it into your database depends heavily on what you're trying to accomplish and your database's schema. 有了该数组后,将其插入数据库将在很大程度上取决于您要完成的工作以及数据库的架构。

Hope that helps. 希望能有所帮助。

Also, in fairness, this is exactly what @Fred meant in his comment. 同样,公平地说,这正是@Fred在他的评论中的意思。

Edit 1 编辑1

To make the javascript work with the change of the input name, you'll need to update all the places in your javascript where you were referencing the name of the input to the new name (chkboxarray[]). 为了使javascript能够使用输入名称的更改,您需要将javascript中将输入名称引用到的所有位置更新为新名称(chkboxarray [])。

The resulting code should look like this: 产生的代码应如下所示:

<script type="application/javascript">
    function check_uncheck(truefalse) {
        var boxes = document.forms[0]["chkboxarray[]"].length;
        var form = document.getElementById('checkForm');
        for (var i = 0; i < boxes; i++) {
            if (truefalse) {
                form["chkboxarray[]"][i].checked = true;
            } else {
                form["chkboxarray[]"][i].checked = false;
            }
        }
    }
</script>

I've created a fiddle to show this works for checking/unchecking all the boxes: https://jsfiddle.net/solvomedia/3Ln468u3/ 我创建了一个小提琴以显示此作品可用于选中/取消选中所有框: https : //jsfiddle.net/solvomedia/3Ln468u3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM