0

我想要几个单选按钮来显示动态网页的信息。我有(在 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";
}

我没有收到任何错误,但也没有任何反应。非常感谢。

编辑:这就像我要问的:php中的单选按钮值

4

2 回答 2

5

因为您的单选按钮的名称属性设置为“动态”,所以我想您必须尝试以下操作:

if(isset($_GET["dynamic"])) {
    // do something here
    echo $_GET["dynamic"];
}
于 2012-07-16T14:53:13.217 回答
1

PHP 通过它们的“名称”属性访问表单变量,而不是它们的“值”属性。为了检索所选单选按钮的值,您可以使用$_GET['dynamic']and not $_GET['home']or $_GET['site1']or $_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>
于 2012-07-16T15:43:06.123 回答