我想有几个单选按钮来显示动态网页的信息。我有(在PHP中):
echo '
<html>
<head>
<title> Dynamic PHP </title>
</head>
<body>
<form action="dynamic.php" method="get">
<input type="radio" name="dynamic" value="home" checked> Home </br>
<input type="radio" name="dynamic" value="site1"> Site 1 </br>
<input type="radio" name="dynamic" value="site2"> Site 2
</form>
</body>
</html>
';
if (isset($_GET["home"])){
echo "Home";
}
if (isset($_GET["site1"])){
echo "Site 1";
}
if (isset($_GET["site2"])){
echo "Site 2";
}
我没有任何错误,但也没有任何反应。非常感谢。
编辑:这就像我问的那样:radio button value in php
答案 0 :(得分:5)
因为你的radiobuttons的name属性设置为“dynamic”,我想你必须尝试这个:
if(isset($_GET["dynamic"])) {
// do something here
echo $_GET["dynamic"];
}
答案 1 :(得分:1)
PHP通过“name”属性访问表单变量,而不是“value”属性。要检索所选单选按钮的值,您可以使用$_GET['dynamic']
而不是$_GET['home']
或$_GET['site1']
或$_GET['site2']
。
因此,假设此页面名为dynamic.php,则回显所选页面的代码为:
if(!empty($_GET['dynamic'])){
echo $_GET['dynamic'];
}
希望这有帮助!
编辑:为了回显所选的选项:
<?php
if(!empty($_GET['dynamic'])){
$selected = $_GET['dynamic'];
}
else{
//if no option was selected, set home as default
$selected = 'home';
}
?>
<form action="dynamic.php" method="get">
<input type="radio" name="dynamic" value="home" /> Home <?php echo ($selected == 'home' ? 'This was selected!' : '');?> </br>
<input type="radio" name="dynamic" value="site1" /> Site 1 <?php echo ($selected == 'site1' ? 'This was selected!' : '');?> </br>
<input type="radio" name="dynamic" value="site2" /> Site 2 <?php echo ($selected == 'site2' ? 'This was selected!' : '');?> </br>
</form>