Skip to content
Advertisement

Displaying radio button choice php?

My code works fine after the form fill out all values are print accept my gender value.

Here in the code below is an example imagine firstName,lastName…. the $row[dob] value prints out successfully.

But the option for the gender is not printing is there some array function i need to use for this to have the option selected bu the user print back on the screen when only display the details of the user (similar to a sticky form)?

    <li><label for='dateOfBirth'>DOB:</label>
    <input type='date' name='dateOfBirth' value = $row[dob] min='1900-01-01' max= '2015-01-01'/></li>

    <li>
        <input type='radio' name='gender' value=$row[gender]'>
        <label for='male'>Male</label><br>
        <input type='radio' name='gender' value='female'>
        <label for='female'>Female</label><br>
    </li>

Advertisement

Answer

you need to use the checked attribute

if the value that comes from $row['gender'] is male check the male radio otherwise check the female

like this

<li>
     <input type='radio' name='gender' <?= $row[gender] == 'male'? 'checked':'' ?> value='male'>
     <label for='male'>Male</label><br>
     <input type='radio' name='gender' <?= $row[gender] == 'female'? 'checked':'' ?> value='female'>
     <label for='female'>Female</label><br>
</li>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement