Home > php教程 > php手册 > body text

PHP判断表单复选框选中状态完整例子

WBOY
Release: 2016-06-13 09:31:48
Original
912 people have browsed it

网页表单中经常用到复选框,那么如何用PHP来判断提交的表单中哪些复选框被选中,并读取其中的数据呢。

首先建立一个表单:form.html

复制代码 代码如下:


3

7

1

15



然后建立一个处理表单的程序:checkbox.php

复制代码 代码如下:

$a=$_POST["s"];
print_r($a);
?>


这个可以用数组的形式显示出,哪些被选中了。显示结果类似于:Array( [0]=7 [1]=15 )

但是上面这个程序只是用来显示复选框是否正常,如果逐个取出数组中所有的数据,需要用到循环。

所以进一步将程序修改为:checkbox.php

复制代码 代码如下:


$a=$_POST["s"];
for($i=0;$i {
echo "选项".$a[$i]."被选中
";
}
?>


这样执行的结果类似于:

复制代码 代码如下:

选项3被选中
选项15被选中

相信下一步具体执行其他的语句就方便多了吧。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!