How to check database and, as a result, check a radio button?

I have these profiles saved in a database with lots of random information like name, gender, age, etc.

The user can edit their profile by clicking a hyperlink and it takes them to a form much like the one they filled out when they first registered. I've designed it so all the text input fields already have the values they had previously filled in. Here's an example of what one looks like:

<input type="text" name="fname" value="<?php echo $result['firstName']; ?>"/>

As you can see I'm echoing their name from the array I created from querying the database. But the problem is I used radio buttons (and drop down boxes too) for some of these inputs like gender.

So how do I check the appropriate radio button once I establish they're male or female from the database?

Answers:

Answer
<input type="radio" name="gender" value="m" <? if($row['gender'] == "m") print "selected";?> >
<input type="radio" name="gender" value="f" <? if($row['gender'] == "f") print "selected";?> >

i think it's simplier. because they can;t be both, at the same time.

Answer

Very easily:

<input type="radio" name="gender" value="M" <?php echo ($result['gender'] == "M" ? 'checked="checked"': ''); ?> />
<input type="radio" name="gender" value="F" <?php echo ($result['gender'] == "F" ? 'checked="checked"': ''); ?> />

Same thing for a select box, when listing the options check and see if their stored value is the same as the value of the option you're listing.

Answer

something like this:

if($result['gender'] == 'male')
{
    echo '<input type="radio" name="gender" value="male" checked="checked"> Male';
    echo '<input type="radio" name="gender" value="female"> Female'; 
}
else {
    echo '<input type="radio" name="gender" value="male"> Male';
    echo '<input type="radio" name="gender" value="female" checked="checked"> Female';
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.