提交页 |
//js控制全选/取消全选 |
<script type= "text/javascript" > |
function check_all(obj,cName) |
{ |
var checkboxs = document.getElementsByName(cName); |
for ( var i=0;i<checkboxs.length;i++){checkboxs[i].checked = obj.checked;} |
} |
</script> |
|
|
<form id= "form1" name= "form1" method= "post" action= "receive.php" > |
<table width= "100%" ><tr> |
<td></td> |
<td colspan= "2" > |
<div align= "center" > |
<p><input type= "checkbox" name= "all" onclick= "check_all(this,'checkbox[]')" />全选/全不选</p> |
<input type= "checkbox" name= "checkbox[]" value= "item1" /> |
1 |
<input type= "checkbox" name= "checkbox[]" value= "item2" /> |
2 |
<input type= "checkbox" name= "checkbox[]" value= "item3" /> |
3 |
</div> |
</td> |
<td></td></tr> |
<tr><td></td> |
<td colspan= "2" > |
|
<div align= "center" > |
<input type= "submit" name= "Submit" value= "提交" /> |
</div> |
</td> |
<td></td></tr> |
</table> |
|
如果名字不用数组的话,php只能获取到最后一个checkbox的值 |
|
receive.php |
$checkbox = $_POST [ 'checkbox' ]; |
|
/*如果要获取第一个数值就直接中止输出,使用下面代码,相当于radiobox了>.<不过有点特殊而已。。*/ |
for ( $i =0; $i <= count ( $checkbox ); $i ++) |
{ |
if (! is_null ( $checkbox [ $i ])) |
{ $chechvalue = $checkbox [ $i ]; break ;} |
} |
echo $chechvalue ; |
|
/*如果要获取全部数值则使用下面代码*/ |
for ( $i =0; $i <= count ( $checkbox ); $i ++) |
{ |
if (! is_null ( $checkbox [ $i ])) |
{ $chechvalue .= $checkbox [ $i ]} |
} |
echo $chechvalue ; |
转自:http://www.cnblogs.com/freespider/archive/2011/03/18/1988308.html