retrieve checkbox,radio and drop down value from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
supriya
Forum Newbie
Posts: 1
Joined: Mon Mar 21, 2011 5:23 am

retrieve checkbox,radio and drop down value from database

Post by supriya »

my html form consists of drop down for selecting city and state.
radio buttons for selecting gender.
and a few check boxes.

the form is filled and the values are stored in a database table.

I am working on a php script, which retrieves values from database into an array.

consider that my array's first element corresponds to a radio button with value M ( M- male)

then i want the radio button for male to be selected.

similarly if a particular city is selected the i want that city selected in the drop down menu...

this is similar to web sites to which we are registered allow us to update our profiles by displaying the fields with the old values....

your response will be of great help to me...
thanking you guys in advance!!
divedj
Forum Commoner
Posts: 47
Joined: Wed Dec 29, 2010 4:32 am
Location: Malta

Re: retrieve checkbox,radio and drop down value from databas

Post by divedj »

To display data pulled of a db in a drop down list you just check if your array holds the needed variable and display it in the list like I use in my country example below.

Code: Select all

<?php
//your mysql connections and query  here

$data = mysql_fetch_array($res);

?>

<form action="call the same script" method="post" >

<!-- whatever formfields you have--!>

<select name="country">
    <?php
         if(!empty($data['country'])
         {
             echo "<option value=\"".$data['country']."\">".$data['country']."</option>"; //if there is an entry in the db this will be displayed first
         }
    ?>
    <option value="Afganistan">Afghanistan</option>
    <option value="Albania">Albania</option>
    <option value="Algeria">Algeria</option>
    <option value="American Samoa">American Samoa</option>
    <option value="Andorra">Andorra</option>
</select>

<!--A quick example how to deal with a checkbox  --!>
<input type="checkbox" name="option" value="checkbox" <?php if($data['option']=='1'){ echo "checked=\"true\""; }?>>

</form>

A very good explanation how to deal with radio buttons you find here http://www.homeandlearn.co.uk/php/php4p10.html.

Hope I could give you a couple of ideas
Post Reply