php代码获取表单中的checkbox值

测试代码如下:

<?php

if($_POST['submit']){
foreach($_POST['test'] as $box){
echo $box."<br>";
}
}
?>


<!DOCTYPE html>  
<html>  
<head>
<meta charset="utf-8">  
<style>  
  
</style>  
<title>php获取复选框被选中的值</title>  
</head>  
<body>  
<form action="" method="post">
<input type="checkbox" name="test[]" value="0" />0&nbsp;&nbsp;  
<input type="checkbox" name="test[]" value="1" />1&nbsp;&nbsp;  
<input type="checkbox" name="test[]" value="2" />2&nbsp;&nbsp;  
<input type="checkbox" name="test[]" value="3" />3&nbsp;&nbsp;  
<input type="checkbox" name="test[]" value="4" />4&nbsp;&nbsp;  
<input type="checkbox" name="test[]" value="5" />5&nbsp;&nbsp;  
<br/>
<input type="submit" name="submit"  value="提  交" />  
</form>
</body>  
</html> 
</body>

</html>


主要注意点:checkbox的name命名要记得带上[],用$_POST[]方法获取checkbox数组对象时要记得将[]去掉,否则得不到值。

你可能感兴趣的:(PHP,获取checkbox的值)