我是一名初学者。我在一张桌子上显示了10名学生的学生证和学生姓名。对每个学生ID,应该有一个checkbox
(动态)。当我点击ADD按钮时,所有查看的学生详细信息(ID,姓名)必须插入另一个database
table
。我该怎么办?如何在php中获取多个选中(动态)复选框的值?
1
A
回答
2
使用复选框名称为数组, 例如:
<form method="post" action="" id="frm_id">
<input type="checkbox" name="chkid[]" value="10,Anu" />Anu
<input type="checkbox" name="chkid[]" value="11,Raj" />Raj
<input type="checkbox" name="chkid[]" value="12,Ram" />Ram
<input type="checkbox" name="chkid[]" value="13,xxx" />xxx
<input type="checkbox" name="chkid[]" value="14,yyy" />yyyy
<input type="checkbox" name="chkid[]" value="15,zzz" />zzz
<input type="checkbox" name="chkid[]" value="16,qqqq" />qqqq
<input type="submit" value="Insert" name="sub"/>
</form>
<?php
if(isset($_POST['sub']))
{
$id=$_POST['chkid'];
for($i=0;$i<count($id);$i++)
{
$exp=explode(',',$id[$i]);//Explode id and name
echo 'id='.$exp[0].',Name='.$exp[1];echo "<br>";
echo $query="INSERT INTO tbl_student (id,name) values ('$exp[0]','$exp[1]')";echo "<br><br>";
}
}
?>
0
尝试使用复选框元素的数组是这样的:
<input type="checkbox" name="months[]" value="feb">February<br>
<input type="checkbox" name="months[]" value="mar">March<br>
<input type="checkbox" name="months[]" value="apr">April<br>
1
<form method="post" action="pageurl">
<input type="checkbox" name="studentid[]" value="1,Student1" />Student1
<input type="checkbox" name="studentid[]" value="2,Student2" />Student2
<input type="checkbox" name="studentid[]" value="3,Student3" />Student3
<input type="checkbox" name="studentid[]" value="4,Student4" />Student4
<input type="submit" />
</form>
<?php
$id=$_POST['studentid'];
foreach($id as $student)
{
$extract = explode(',',$student);
$query="INSERT INTO student (id,name) values ('$extract[0]','$extract[1]')";
}
?>
+0
**危险**:你很容易[SQL注入攻击](http://bobby-tables.com/)**,你需要[防守](http ://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己从。 – Quentin 2014-10-07 06:44:51
相关问题
- 1. 如何获取多个选中的复选框值?
- 2. 从复选框中获取多个值
- 3. 取消多个复选框中的复选框选择状态
- 4. 如何在javascript中获取多个复选框的值
- 5. 如何从asp.net中的多个复选框中获取值?
- 6. 如何获取HTML.ActionLink中一个或多个复选框的值?
- 7. 如何获得在PHP中选中的复选框的值?
- 8. 从复选框jQuery获取动态值
- 9. 选择多个复选框,并在Python中获取值Django
- 10. 如何获取在Grid.MVC中选中的复选框行值
- 11. 如何从Django的复选框中获取多个值
- 12. 如何获取AngularJS中多个复选框的值
- 13. 如何从fcbk多选中动态创建的选择框中获取值?
- 14. jQuery:多个复选框,获取唯一被选中的值
- 15. 获取选中的复选框值
- 16. Android:获取选中的复选框值
- 17. 获取多个复选框值,并传递给PHP jQuery中
- 18. 在c中获得多个复选框选中的值#
- 19. 如何从AJAX复选框中获取多个值?
- 20. 在Angular2中获取动态列表中的复选框状态
- 21. 如何动态创建复选框并获取所有选中的复选框的角度值2
- 22. 如何获取在MVC3中选中的一个复选框?
- 23. 如何根据状态选中/取消选中另一个复选框来选中/取消选中复选框
- 24. 如何在选中多个复选框后禁用复选框?
- 25. 如何在android中动态获取复选框?
- 26. 如何获取C中未选中复选框的值#
- 27. 如何获取Django request.POST中所有选中复选框的值?
- 28. 如何从多个选择选项中获取多个选定的值php codeigniter?
- 29. 如何在android中的多选复选框中获取关键值对复选框
- 30. 如何获取动态复选框的值?
你有什么迄今所做? – roullie 2014-10-07 05:27:19