在php编程中,对多选框checkbox的取值方式,主要借助于php数组的形式。
比如下面这个html页面,内容:
<FORM method="post" action="/blog_article/checkTest.html"> <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
//取checkbox元素值
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>php取checkbox多选框的值_www.</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." />
</label>
<label>
<input type="checkbox" name="checkbox[]" value="" />
</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.
[3] =>
)
*/
?>首先,来看下checkbox复选框值的方法。
checkbox在php读取值时要用数组。
例1,
<html xmlns="http://www./1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>php获取 checkbox复选框值的方法_www.</title>
</head>
<body>
<form name="form1" method="post" action="">
<label>
<input type="checkbox" name="checkbox[]" value="复选一">
复选一
</label>
<label>
<input type="checkbox" name="checkbox[]" value="复选二">
</label>
复选二
<label>
<input type="checkbox" name="checkbox[]" value="复选三">
</label>
复选三
<label>
<input type="checkbox" name="checkbox[]" value="复选四">
</label>
复选四
<label>
<input type="submit" name="Submit" value="提交">
</label>
</form>
</body>
</html>
<?
if( $_POST )
{
$value = $_POST['checkbox'];
echo '你选择了:'.implode(',',$value);
//由于checkbox属性,必须把checkbox复选择框的名字设置为一个如果checkbox[]。
}
?>
例2,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>php获取多选框checkbox值_www.</title>
</head>
<body>
<?php
$area_arr = array();
if($_GET['action']=="submit"){
$area_arr = $_POST['area'];
}
echo "您选定的地区为: ";
foreach ($area_arr as $k=>$v){
echo $v." ";
}
?>
<form id="form1" name="form1" method="post" action="/blog_article/action/submit.html">
<p>河北
<label>
<input type="checkbox" id="area" name="area[]" value="河北">
</label>
</p>
<p>河南
<label>
<input type="checkbox" id="area[]" name="area[]" value="河南">
</label>
</p>
<p>山西
<label>
<input type="checkbox" id="area[]" name="area[]" value="山西">
</label>
</p>
<p>山东
<label>
<input type="checkbox" id="area[]" name="area[]" value="山东">
</label>
</p>
<p>江苏
<label>
<input type="checkbox" id="area[]" name="area[]" value="江苏">
</label>
</p>
<p>浙江
<label>
<input type="checkbox" id="area[]" name="area[]" value="浙江">
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="提交">
</label>
</p>
</form>
</body>
</html>
例3,
php checkbox默认选择问题,用if判断即可,如下:
<input name="xxx" type="checkbox" value="jiaju" <?php if($myrow[fujia_jiaju]) echo("checked");?>>
有关php中获取checkbox复选框值的方法,就介绍这些了。希望对大家有所帮助。
本节介绍下php伪造HTTP_REFERER的方法。
环境:Apache/2.2.8 + PHP/5.2.5 + Windows XP系统,本地测试。
首先,在网站虚拟根目录下建立1.php和2.php两个文件。
1,文件 1.php
<?php
/**
* 伪造http_referer来源
* edit www.
* at 2013/7/4
*/
$host = '127.0.0.1';
$target = '/2.php';
$referer = 'http://www.'; //伪造HTTP_REFERER地址
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if (!$fp){
echo "$errstr($errno)<br />\n";
}
else{
$out = "
GET $target HTTP/1.1
Host: $host
Referer: $referer
Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)){
echo fgets($fp, 1024);
}
fclose($fp);
}
?>
2,文件 2.php
读取当前的HTTP_REFERER服务器值。
<?php echo "<hr />"; echo $_SERVER["HTTP_REFERER"]; ?>
执行1.php文件,打开http://localhost/1.php,页面返回信息如下:
Content-Type: text/html; charset=gb2312
http://www.
从以上结果可知,伪造来源HTTP_REFERER信息是成功的。
因此,如果仅仅是判断HTTP_REFERER,是不安全的,可以考虑在验证页中加上验证码;再结合IP判断就更安全了。
补充,ASP下下伪造来源,可以参考如下代码:
dim http
set http=server.createobject("MSXML2.XMLHTTP") '//MSXML2.serverXMLHTTP也可以
Http.open "GET",url,false
Http.setRequestHeader "Referer","http://www./"
Http.send()
%>
有关php中依靠来源http_referer的例子,就介绍到这里了,最后又介绍了一个asp中伪造来源http_referer的例子。
希望以上的内容,对您有所帮助。