HTML表单代码:
php 实例之如何获取radio单选按钮的值Please enter your first name:
Please select your favorite color wine:
White
Ros
Red
Please enter your favorite dish:
在表单中,如果多个radio的name值相同,我们视为这些radio为一组单选按钮。
从代码可以看出,该表单是以post方式提交的,并且该表单创建了一组radio单选按钮,他们的name属性值都为color。
php代码:
$username = $_POST['username'];
$color = $_POST['color'];
$dish = $_POST['dish'];
if( $username != null )
{
echo( "Thanks for your selection $username
" );
}
if( ( $color != null ) && ( $dish != null ) )
{
$msg = "You really enjoy $dish
";
$msg .= "- especially with a nice $color wine";
echo( $msg );
}
?>
因为表单是以post方式提交,所以,在获取radio单选按钮的值时,使用了$_POST['color']。