php获取checkbox数组的表单数据

提交表单的时候,对于checkbox多选框,name=“field[]”,此时php获取的数组为:从0开始的索引数组;如果name=“field[n]” 有数字n,那么php获取的name数组的索引为n,而不是从0开始的;

代码:

<html xmlns="http://www.jb51.net/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>php获取 checkbox复选框值的方法</title> 
</head> 
<body>
<form name="form1" method="post" action=""> 
<label> 
<input type="checkbox" name="checkbox[11]" value="复选一"> 
复选一 
</label> 
<label> 
<input type="checkbox" name="checkbox[22]" value="复选二"> 
</label> 
复选二 
<label> 
<input type="checkbox" name="checkbox[33]" value="复选三"> 
</label> 
复选三 
<label> 
<input type="checkbox" name="checkbox[44]" value="复选四"> 
</label> 
复选四 
<label> 
<input type="submit" name="Submit" value="提交"> 
</label> 
</form> 
</body> 
</html>

php:

if( $_POST ) 
{ 
    var_dump($_POST['checkbox']);
} 

打印结果为:

array(4) {
  [11]=>
  string(9) "复选一"
  [22]=>
  string(9) "复选二"
  [33]=>
  string(9) "复选三"
  [44]=>
  string(9) "复选四"
}
 

此时[]里面的数字,可以放一些动态数据,比如id,后台再处理

posted on 2017-03-30 12:08  飞哥100  阅读(6693)  评论(0编辑  收藏  举报

导航