Jump to content

checkbox selected question


princeofpersia

Recommended Posts

Hi guys, im having a radio button which i need it to be checked if its status is equal to something.

 

as an example i need to use the checkbox below checked if its field in mysql is equal to male

 

     <p>Male
                <input type="radio" name="male" value="Male"/>
                <br/>

 

 

this is made for a profile page where users can insert their gneder, I know i have to return the value from myslq to check if its equal to radio button value, I can do select mysql_query from my db but how should i say if its match, check the radio button?

can u please help me how to do it?

Link to comment
https://forums.phpfreaks.com/topic/223897-checkbox-selected-question/
Share on other sites

One way to do it is to have PHP generate the HTML for each of the radio buttons. As it generates each of the buttons it would check the database to determine if the person is Male or Female. Based on this info, it would generate a "checked" button that contains the attribute: checked="checked". For example (pseudocode):

 

$profiles = ... // Assume you retrieved all of your profiles as a collection of objects from your database
foreach($profiles as $profile)
{
    if($profile->gender == 'male')
    {
        echo '<input type="radio" name="male" value="Male" checked="checked" />';
        echo '<input type="radio" name="female" value="Female" />';
    }
    else
    {
        echo '<input type="radio" name="male" value="Male" />';
        echo '<input type="radio" name="female" value="Female"  checked="checked" />';
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.