如何将复选框数组中的数据保存到数据库中
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
我正在尝试将从清单发送的数据保存到数据库,但我卡住了。我正在考虑使用 foreach,但我不知道要放入什么。
我想把它写成:
foreach($_POST['id'] as $add){
insert into database...
}
我该怎么做?
如果我按照 Fred-ii 和 xjstratedgebx 的建议进行更改 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>
如果您将复选框的名称从 "chkboxarray" 更改为 "chkboxarray[]",那么在提交表单时选中的任何复选框都会将其值作为数组传递给键下的服务器"chkboxarray".
基本上,更改此行:
echo '<input type="checkbox" name="chkboxarray" value="'.$id.'" />'.$concate.'<br />';
收件人:
echo '<input type="checkbox" name="chkboxarray[]" value="'.$id.'" />'.$concate.'<br />';
因此,如果您 var_dump $_POST
超级全局,您应该看到如下内容:
array(1) {
[chkboxarray]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "4"
}
}
在上面的示例中,id 的 3 和 4 的复选框被选中,因此它们被发送到服务器。
获得该数组后,将其插入数据库在很大程度上取决于您要完成的任务和数据库的架构。
希望对您有所帮助。
此外,公平地说,这正是@Fred 在他的评论中的意思。
编辑 1
要使 javascript 与输入名称的更改一起工作,您需要更新 javascript 中您引用新输入名称的所有位置名称 (chkboxarray[]).
生成的代码应如下所示:
<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>
我创建了一个 fiddle 来展示这适用于 checking/unchecking 所有框:https://jsfiddle.net/solvomedia/3Ln468u3/
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
我正在尝试将从清单发送的数据保存到数据库,但我卡住了。我正在考虑使用 foreach,但我不知道要放入什么。
我想把它写成:
foreach($_POST['id'] as $add){
insert into database...
}
我该怎么做?
如果我按照 Fred-ii 和 xjstratedgebx 的建议进行更改 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>
如果您将复选框的名称从 "chkboxarray" 更改为 "chkboxarray[]",那么在提交表单时选中的任何复选框都会将其值作为数组传递给键下的服务器"chkboxarray".
基本上,更改此行:
echo '<input type="checkbox" name="chkboxarray" value="'.$id.'" />'.$concate.'<br />';
收件人:
echo '<input type="checkbox" name="chkboxarray[]" value="'.$id.'" />'.$concate.'<br />';
因此,如果您 var_dump $_POST
超级全局,您应该看到如下内容:
array(1) {
[chkboxarray]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "4"
}
}
在上面的示例中,id 的 3 和 4 的复选框被选中,因此它们被发送到服务器。
获得该数组后,将其插入数据库在很大程度上取决于您要完成的任务和数据库的架构。
希望对您有所帮助。
此外,公平地说,这正是@Fred 在他的评论中的意思。
编辑 1
要使 javascript 与输入名称的更改一起工作,您需要更新 javascript 中您引用新输入名称的所有位置名称 (chkboxarray[]).
生成的代码应如下所示:
<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>
我创建了一个 fiddle 来展示这适用于 checking/unchecking 所有框:https://jsfiddle.net/solvomedia/3Ln468u3/