PHP表单单选按钮

2018-02-22 16:40 更新

PHP教程 - PHP表单单选按钮

单选按钮用于从多个选项中进行单选。组中的所有单选按钮都具有相同的name属性。

每个组只能选择一个按钮。与复选框一样,使用value属性来存储发送到服务器的值(如果)按钮。

value属性对于复选框和单选按钮是必需的,并且对于其他字段类型是可选的。

<label for="radioButtonField1">A radio button field</label> 
<input type="radio" name="radioButtonField" id="radioButtonField1"  value="radio1" /> 
<label for="radioButtonField2">Another radio button</label> 
<input type="radio" name="radioButtonField" id="radioButtonField2"  value="radio2" /> 

您可以使用与预选复选框相同的方法预先选择单选按钮。



例子

以下脚本用于index.htm。它有一组单选按钮。

<html> 
 <body>                                  
  <form action="index.php" method="post">                                                
   <b>Please select your favorite color wine:</b> <br>             
   <input type="radio" name="color" value="white">  White           
   <input type="radio" name="color" value="rose">   Rose
   <input type="radio" name="color" value="red">    Red <br>
   <input type="submit" value="Submit This Form">
 </form>            
 </body>
</html>

以下脚本用于index.php。 它从上面的表单中读取数据。


<?php
  $color = $_POST["color"];
  if( ( $color != null ) )
  {
    $msg .= "a nice $color ";
    echo( $msg );
  }
?>


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号