php checkbox 取值详细说明


设我们有一个html页面,代码如下:
复制代码 代码如下:

<FORM method="post" action="checkTest.php">
<INPUT name="test[]" type="checkbox" value="1" />
<INPUT type="checkbox" name="test[]" value="2" />
<INPUT type="checkbox" name="test[]" value="3" />
<INPUT type="checkbox" name="test[]" value="4" />
<INPUT type="checkbox" name="test[]" value="5" />
<INPUT type="submit" name="Submit" value="Submit" />
</FORM>

注意上面input的name属性,各个属性内容都一样,而且都是test[],加上[]的原因在于让test的内容变成数组形式传递。
checkTest.php的代码内容如下:
复制代码 代码如下:

<?php
echo implode(",",$_POST['test']);
?>

我们输出内容时只需要注意利用implode函数将数组内容转化为字符串即可。
注:该功能可在删除多记录等场合运用。如Delete from tbl where ID in (implode(",",$_POST['test']))即可。
实例代码:
复制代码 代码如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
html复选框如果要以数据组形式发送给php脚本处理就必须以如checkbox[]这形式
<form id="form1" name="form1" method="post" action="">
<label>
<input type="checkbox" name="checkbox[]" value="1" />
</label>
<label>
<input type="checkbox" name="checkbox[]" value="2" />
</label>
<label>
<input type="checkbox" name="checkbox[]" value="www.phpstudy.net" />
</label>
<label>
<input type="checkbox" name="checkbox[]" value="phpstudy.net" />
</label>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
</form>
</body>
</html>
<?
//判断是否点击提交
if( $_POST )
{
$array = $_POST['checkbox'];
print_r($array);
}
/*
结果:
Array
(
[0] => 1
[1] => 2
[2] => www.phpstudy.net
[3] => phpstudy.net
)
简单的很多事情在做之前觉得复杂但做起来就很容易了,像这个复选框代码就是这样了。
*/
?>

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3