Question

I want to be able to have different drop down menues appear when radio buttons are checked. Here is the code I have so far:

        <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>;
        }          
        ?>     
Was it helpful?

Solution

Try

<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 }} ?>   

This will only work if you have posted the result of the "gender" radio, of course, since php is executed on server-side...

OTHER TIPS

Try this:

Don't render HTML as a string in PHP.

Rather than break PHP code segment and write HTML code.

So, following is the correct answer:

<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
}          
?> 

Write this line at the top of your php file you will not get Notice: Undefined index: gender

<?php
error_reporting(0);
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top