help with drop down list selected value

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
greedyisg00d
Forum Commoner
Posts: 42
Joined: Thu Feb 12, 2009 2:48 am

help with drop down list selected value

Post by greedyisg00d »

Hi I have a drop down list which the value is stored in database when I submit the form

Code: Select all

]<select name="type" id="Type">
            <option selected="selected" value="">[Select one]</option>
            <option value="Meeting">Meeting</option>
            <option value="Conference">Conference</option>
            <option value="Workshop">Workshop</option>
          </select>
For example the user greedyisgood selects Meeting and store it in the database. My problem is I have an edit page and what I want is the value of dropdown in the database will now be selected in the edit page like this

Code: Select all

]<select name="type" id="Type">
            <option value="">[Select one]</option>
            <option selected="selected" value="Meeting">Meeting</option>
            <option value="Conference">Conference</option>
            <option value="Workshop">Workshop</option>
          </select>
Any idea on how it works? Thanks in advance
longshoremoney
Forum Newbie
Posts: 3
Joined: Sun Feb 15, 2009 11:33 pm

Re: help with drop down list selected value

Post by longshoremoney »

When you populate the drop down list, for the value, did you manually enter that data (Meeting, Etc.)? You usually want to populate it with records from a table in the database because they must have IDs which are unique. These IDs can be the option value and the other field can be the name associated to the value for an html form.

Example:
________________
Table: People

ID Name
1 Michael
2 Renee
________________

You could populate the dropdown list with this:

Code: Select all

 
 
$query = "SELECT * FROM people";
$result = mysql_query($query) or die (mysql_error());
 
 
.....omit some code, you start your html form, the next part is for the drop down list...
 
 
<select name="name">
<?php
while($row = mysql_fetch_assoc($result)){
echo "<option value=".$row['ID'].">".$row['Name']."</option>";
}
?>
</select>
This would show Michael and when you selected this and submitted your form, it would really send a value of 1. If you picked Renee, it would send a value of 2.

Make sense?

You would want this because then you know exactly which record you can edit in your next script based on the unique ID of the record. Hope this helps...
Post Reply