Forum Moderators: coopster

Message Too Old, No Replies

How to populate radio boxes

Php? Javascript?

         

sanblasena

3:13 pm on Apr 8, 2006 (gmt 0)

10+ Year Member



Hello,

I have a form with radio boxes - when I display the form I read the data from a database and put it in the appropriate input boxes - but I have not figured out a way to populate the radio boxes (or dropdowns) - for example if the database says the person is female I would like the radio box for female checked.
I am using MySQL/PHP

Thanks for you help.

Pat

Birdman

3:42 pm on Apr 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do this:

  1. Put the db value into a variable
    $db_gender = [i]whatever db syntax you use here[/i]
    ;
  2. create empty var for HTML
    $radio_inputs = '';

  3. Create array of radio values
    $radio = array('Male', 'Female');

  4. Loop the array, comparing them with the db value
    foreach ($radio as $gender) {
    $checked = ($db_gender == $gender)? ' checked="checked"' : '';
    $radio_inputs .= '<input type="radio" name="gender" value="'.$gender.'"' . $checked . ' />';
    }

  5. Output the HTML
    print $radio_inputs;