php表单中如何获取单选按钮与复选按钮的值(示例)

转载: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]."&nbsp;&nbsp;";
        }
}
?>

我们要学会用数组来解决我们的问题!!!!

以上就是php表单中如何获取单选按钮与复选按钮的值(示例)的详细内容,更多请关注php中文网其它相关文章!

转载于:https://www.cnblogs.com/hao-1234-1234/p/6742249.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例代码,您可以在此基础上自行修改和完善: ``` <!DOCTYPE html> <html> <head> <title>User Profile Form</title> </head> <body> <h1>User Profile Form</h1> <form action="submit.php" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required><br><br> <label for="gender">Gender:</label> <input type="radio" id="male" name="gender" value="male" required> <label for="male">Male</label> <input type="radio" id="female" name="gender" value="female"> <label for="female">Female</label><br><br> <label for="interests">Interests:</label> <input type="checkbox" id="music" name="interests[]" value="music"> <label for="music">Music</label> <input type="checkbox" id="sports" name="interests[]" value="sports"> <label for="sports">Sports</label> <input type="checkbox" id="travel" name="interests[]" value="travel"> <label for="travel">Travel</label><br><br> <label for="country">Country:</label> <select id="country" name="country"> <option value="USA">USA</option> <option value="China">China</option> <option value="Japan">Japan</option> <option value="Korea">Korea</option> </select><br><br> <label for="message">Message:</label> <textarea id="message" name="message" rows="5" cols="30"></textarea><br><br> <input type="submit" value="Submit"> </form> </body> </html> ``` 这个表单包含了文本框、单选按钮复选框、下拉式列表框以及文本区域。其,文本框和单选按钮都是必填项,而复选框、下拉式列表框和文本区域是可选项。在提交表单后,表单数据将被传递到 `submit.php` 文件进行处理。下面是 `submit.php` 文件的示例代码: ``` <!DOCTYPE html> <html> <head> <title>User Profile</title> </head> <body> <h1>User Profile</h1> <table> <tr> <th>Name:</th> <td><?php echo $_POST["name"]; ?></td> </tr> <tr> <th>Gender:</th> <td><?php echo $_POST["gender"]; ?></td> </tr> <tr> <th>Interests:</th> <td><?php echo implode(", ", $_POST["interests"]); ?></td> </tr> <tr> <th>Country:</th> <td><?php echo $_POST["country"]; ?></td> </tr> <tr> <th>Message:</th> <td><?php echo $_POST["message"]; ?></td> </tr> </table> </body> </html> ``` 在这个文件,我们使用了 PHP 的 `$_POST` 变量来获取表单数据,并将这些数据显示在一个表格。需要注意的是,我们使用了 `implode` 函数来将复选框的合并为一个字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值