How to check database and, as a result, check a ra

2020-05-07 06:58发布

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?

3条回答
太酷不给撩
2楼-- · 2020-05-07 07:17
<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.

查看更多
Ridiculous、
3楼-- · 2020-05-07 07:26

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.

查看更多
虎瘦雄心在
4楼-- · 2020-05-07 07:27

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';
}
查看更多
登录 后发表回答