有不同的下拉菜单?

时间:2014-04-25 11:22:52

标签: php drop-down-menu radio-button

我希望能够在选中单选按钮时显示不同的下拉菜单。这是我到目前为止的代码:

        <input checked="unchecked" type="Radio" name="gender" value="male" class="auto-style3" style="width: 20px">Male
            <input checked="unchecked" type="Radio" name="gender" value="female">Female
                                                <br>                                                                             
            <?php 

        $answer = $_POST['gender'];  
        if ($gender == "male") {          
            <select name="Select1">
    <option></option>
    <option>Floor</option>
    <option>Pommel</option>
    <option>Rings</option>
    <option>Vault</option>
    <option>P Bars</option>
    <option>High Bar</option>
        </select>;      
        }
        else {
            <select name="Select1">
    <option></option>
    <option>Vault</option>
    <option>Uneven Bars</option>
    <option>Beam</option>
    <option>Floor</option>

        </select>;
        }          
        ?>     

3 个答案:

答案 0 :(得分:0)

尝试

<form name="yourform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input checked="unchecked" type="Radio" name="gender" value="male" class="auto-style3" style="width: 20px">Male
<input checked="unchecked" type="Radio" name="gender" value="female">Female
<input type="submit" value="Submit">
</form>

<?php 
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    $answer = $_POST['gender'];  
    if ($answer == "male") {  ?>  

<select name="Select1">
<option></option>
<option>Floor</option>
<option>Pommel</option>
<option>Rings</option>
<option>Vault</option>
<option>P Bars</option>
<option>High Bar</option>
</select>       

    <?php } else { ?>

<select name="Select1">
<option></option>
<option>Vault</option>
<option>Uneven Bars</option>
<option>Beam</option>
<option>Floor</option>
</select>

<?php }} ?>   

只有在您发布了&#34;性别&#34;的结果后,这才有效。无线电,当然,因为php是在服务器端执行的......

答案 1 :(得分:0)

试试这个:

不要在PHP中将HTML呈现为字符串。

而不是破坏PHP代码段并编写HTML代码。

所以,以下是正确答案:

<input checked="unchecked" type="Radio" name="gender" value="male" class="auto-style3" style="width: 20px">Male
<input checked="unchecked" type="Radio" name="gender" value="female">Female
<br>
<?php
$gender = (! empty($_POST['gender'])) ? $_POST['gender'] : 'male';
if ($gender == "male") {
?>       
<select name="Select1">
<option></option>
<option>Floor</option>
<option>Pommel</option>
<option>Rings</option>
<option>Vault</option>
<option>P Bars</option>
<option>High Bar</option>
</select>
<?php
}
else {
?>
<select name="Select1">
<option></option>
<option>Vault</option>
<option>Uneven Bars</option>
<option>Beam</option>
<option>Floor</option>
</select>
<?php
}          
?> 

答案 2 :(得分:-1)

将此行写在您不会得到的php文件的顶部注意:未定义的索引:性别

<?php
error_reporting(0);
?>