4 Replies - 14516 Views - Last Post: 17 March 2010 - 03:04 AM Rate Topic: -----

#1 malaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-August 09

How To Set Default Value From Database For Radio Buttons and Dropdown

Posted 16 March 2010 - 01:24 PM

Hey guys,
Below is the code i hv
Sex:<br/>
<input type="radio" name="sex" value="Milk" checked> Male<br>
<input type="radio" name="sex" value="Butter"> Femail<br>
<br/>
Gift:<br/>
<select name="gift">
<option selected>T-Shirt</option>
<option>Mug</option>
<option>Bear</option>
</select>


From the html, I can simply use 'checked' for radio button and 'selected' from dropdown menu to define certain element as the default value. I don't have problem with that for my registration form. However, i have a problem when it comes to 'update page'.

Basically, i've a database as usual. In the update page, I want to initially insert the previous data he/she inserted. So, first i'll fetch those data by using mysql_fetch_array(). then, for textboxes, i can use something like this:
Name: <input type="text" name="name" value="<?php echo"$row[3]" ?>"><br/>


I can simply straight away place the data into the value's attribute to show it in the textbox. but how to do that for radio button and for dropdown menu? :blink:

Is This A Good Question/Topic? 0
  • +

Replies To: How To Set Default Value From Database For Radio Buttons and Dropdown

#2 spor  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 9
  • Joined: 16-March 10

Re: How To Set Default Value From Database For Radio Buttons and Dropdown

Posted 16 March 2010 - 01:40 PM

View Postmalaya, on 16 March 2010 - 12:24 PM, said:

Hey guys,
Below is the code i hv
Sex:<br/>
<input type="radio" name="sex" value="Milk" checked> Male<br>
<input type="radio" name="sex" value="Butter"> Femail<br>
<br/>
Gift:<br/>
<select name="gift">
<option selected>T-Shirt</option>
<option>Mug</option>
<option>Bear</option>
</select>


From the html, I can simply use 'checked' for radio button and 'selected' from dropdown menu to define certain element as the default value. I don't have problem with that for my registration form. However, i have a problem when it comes to 'update page'.

Basically, i've a database as usual. In the update page, I want to initially insert the previous data he/she inserted. So, first i'll fetch those data by using mysql_fetch_array(). then, for textboxes, i can use something like this:
Name: <input type="text" name="name" value="<?php echo"$row[3]" ?>"><br/>


I can simply straight away place the data into the value's attribute to show it in the textbox. but how to do that for radio button and for dropdown menu? :blink:


Something like:

<input type="radio" name="sex" value="Milk" <? if($row[$x] == "Milk"){echo "checked";} ?> /> Male<br/>
<input type="radio" name="sex" value="Butter" <? if($row[$x] == "Butter"){echo "checked";} ?> /> Female<br/>



Though it might be smoother to load all possible radio options into an associative array first, and then check if a radio should be checked using the array within the foreach loop that would place the radio buttons in the first place.
Was This Post Helpful? 0
  • +
  • -

#3 malaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-August 09

Re: How To Set Default Value From Database For Radio Buttons and Dropdown

Posted 16 March 2010 - 01:44 PM

hhmm... thanks for your reply spor :smile2:

but i think that must be efficient only for small number of form control... how about if i have 5 radio buttons or 100 options in a dropdown menu... it's possible to be :blink:

View Postspor, on 16 March 2010 - 12:40 PM, said:

Something like:

<input type="radio" name="sex" value="Milk" <? if($row[$x] == "Milk"){echo "checked";} ?> /> Male<br/>
<input type="radio" name="sex" value="Butter" <? if($row[$x] == "Butter"){echo "checked";} ?> /> Female<br/>



Though it might be smoother to load all possible radio options into an associative array first, and then check if a radio should be checked using the array within the foreach loop that would place the radio buttons in the first place.

Was This Post Helpful? 0
  • +
  • -

#4 spor  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 9
  • Joined: 16-March 10

Re: How To Set Default Value From Database For Radio Buttons and Dropdown

Posted 16 March 2010 - 01:54 PM

View Postmalaya, on 16 March 2010 - 12:44 PM, said:

hhmm... thanks for your reply spor :smile2:

but i think that must be efficient only for small number of form control... how about if i have 5 radio buttons or 100 options in a dropdown menu... it's possible to be :blink:


That's where the array I mentioned comes in - so the radio buttons could be defined by

<?
$non_pc_radios = array( "Milk"   => "Male",
                        "Butter" => "Female",
                        "Cheese" => "Tranny");
?>



Or you could build this array straight from a db table.

Anyway, you'd then do:

<?
foreach ($non_pc_radios as $value => $text)
{
    echo '<input name="sex" type="radio" ';
    if($row[$x]==$value){
        echo "checked";
    }
    echo ' value="' . $value . '"/>' . $text . '<br/>';
}
?>



Not very elegant code, but hopefully you get the idea ;)
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: How To Set Default Value From Database For Radio Buttons and Dropdown

Posted 17 March 2010 - 03:04 AM

This tutorial does what you are looking for, however it's for a select input box rather than radio.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1