Link to home
Start Free TrialLog in
Avatar of Jazzy 1012
Jazzy 1012

asked on

Put radio button in my form but already has components from my database

            
                             <tr>
                        <td>Gender</td>
                        <td><span id= "keep2"><?php echo $gender; ?></span><input id= "change2" value= "<?php echo $gender; ?>" style= "display:none;" name="gen"></td>
                      </tr>

Open in new window


I have these variables connected to my database, as you can see it displays the gender, and when I click edit it becomes an input value that I can change, However how can I make it that when I click on edit, for $gender instead of showing "female" for example it will show male or female radio button with the button on the gender that is shown in the database, for example for female the button will be on female.
Avatar of arnold
arnold
Flag of United States of America image

Your processing needs to consult the db and set checked on the one in the db.
When db has male
<input type=radio name=gender checked value=male>

<input type=radio name=gender value=Female>

When db has female
<input type=radio name=gender value=male>

<input type=radio name=gender checked value=male>


Ref
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_radio

When you hit edit, your php code needs to output the form with the db item set as checked....
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Correction to above code - left out name attribute of second radio
<tr>
  <td>Gender</td>
  <td>
     <span id= "keep2"><?php echo $gender; ?></span>
     <input id= "change2a" type="radio" value= "male" checked="<?php echo $gender == 'male' ? 'checked': ''; ?>" style= "display:none;" name="gen"/>
     <input id= "change2b" type="radio" value= "female" checked="<?php echo $gender == 'female' ? 'checked': ''; ?>" style= "display:none;" name="gen"/>
  </td>
</tr>

Open in new window

Avatar of Jazzy 1012
Jazzy 1012

ASKER

Yeah I saw that and switched it, thank you