Welcome Guest, Not a member yet? Register   Sign In
radio buttons from database[SOLVED]
#1

[eluser]bobbob[/eluser]
I am sure this has an easy explanation but it is eluding me.

I have a couple of radio buttons with yes and no as the choice:

Code:
<tr>
        <td>Insured</td><td>Yes&lt;input type="radio" name="insured" value="Yes" &lt;?php echo set_radio('insured','Yes'); ?&gt;"&gt;&nbsp;&nbsp;&nbsp;No&lt;input type="radio" name="insured" value="No" &lt;?php echo set_radio('insured','No'); ?&gt;"&gt;&lt;/td>
    </tr>

For signing up this works fine and the form validation works if they didn't choose one.

Now when the user needs to edit their info. Completely different page as the form is now presented populated with their info from the database. When I recreate the form how do I get

$row->insured into the radio button so the correct Yes or No is preselected?
Tx
#2

[eluser]slowgary[/eluser]
You'd need to use the 'checked' attribute. Just add checked='checked' into the input tag that should be checked, as so:
Code:
&lt;input type='radio' name='insured' value='Yes' checked='checked'/&gt;Yes
&lt;input type='radio' name='insured' value='No'/&gt;No

How you do it in PHP is up to you. How about like this? :
Code:
echo ($is_insured == 'Yes') ?
     "&lt;input type='radio' name='insured' value='Yes' checked='checked'/&gt;Yes
      &lt;input type='radio' name='insured' value='No'/&gt;No":

     "&lt;input type='radio' name='insured' value='Yes'/&gt;Yes
      &lt;input type='radio' name='insured' value='No' checked='checked'/&gt;No";
#3

[eluser]bobbob[/eluser]
Thanks slowgary.
Thought there was a simple solution.
This is how I did it:

Code:
<tr>
        <td>Insured</td><td>Yes&lt;input type="radio" name="insured" value="Yes" &lt;?php if($row-&gt;insured == 'Yes') { echo 'checked="checked"';} echo set_radio('insured','Yes'); ?&gt;">&nbsp;&nbsp;&nbsp;No&lt;input type="radio" name="insured" value="No" &lt;?php if($row-&gt;insured == 'No') { echo 'checked="checked"';}  echo set_radio('insured','No'); ?&gt;"></td>
    </tr>

there might well be a simpler way but it works.

Tx




Theme © iAndrew 2016 - Forum software by © MyBB