转载:http://www.php.cn/php-weizijiaocheng-360027.html
php代码中获取表单中单选按钮的值:(单选按钮只能让我们选择一个,这里有一个“checked”属性,这是用来默认选取的,我们每次刷新我们的页面时就默认为这个值。)
例:
1
|
< form name = "myform" action = "" method = "post" > |
性别:
1
2
3
4
5
6
7
|
< input type = "radio" name = "sex" value = "男" checked />男< input name = "sex" type = "radio" value = "女" />女 < input type = "submit" name = "submit" value = "提交" /> </ form > <? php echo "您的选择是:"; echo $_POST["sex"]; ?> |
如果你选择的是男,则出来的值就是“男”,要是你选择的是女,则出来的值就是“女”。
php代码中获取复选框的值:(复选框是能够让我们去多选的,他们同时存在,为了便于传值我们将name令为一个数组。)
格式为:
1
|
< input type = "checkbox" name = "chkbox[]" value = "chkbox1" /> |
方法:在返回页面中用count()函数计算数组的大小,结合for循环语句来输出选择的复选框的值。
例:
1
|
< form action = "" name = "form1" method = "post" > |
您喜欢的数字:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< input type = "checkbox" name = "mrbook[]" value = "1" />1 < input type = "checkbox" name = "mrbook[]" value = "2" />2 < input type = "checkbox" name = "mrbook[]" value = "3" />3 < input type = "checkbox" name = "mrbook[]" value = "4" />4 < input type = "submit" name = "submit" value = "提交" /> </ form > <? php if($_POST['mrbook']!=null) { echo "您选择的数字是:"; for($ i = 0 ;$i<count($_POST['mrbook']);$i++) { echo $_POST['mrbook'][$i]." "; } } ?> |
我们要学会用数组来解决我们的问题!!!!
以上就是php表单中如何获取单选按钮与复选按钮的值(示例)的详细内容,更多请关注php中文网其它相关文章!