Hello,
I would like to create a form which contains a drop down menue (<select><option><option>...) and this menue should display automatically the choice which is read in from a mysql database by PHP.
I have no clue about how to achieve that now. Would be nice if someone can point me to an example.
Dynamic Data in Dropdown menue?
Moderator: General Moderators
-
drcolossus
- Forum Newbie
- Posts: 5
- Joined: Tue Mar 23, 2004 8:19 pm
- Location: Japan
Code: Select all
<?php
echo "<select name = 'Options'>"; //Start selection box
$query = "SELECT * FROM Table"; //Get Data from DB
$result = mysql_query($query) or die("Couldn't Execute Query ".mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "<option value = ".$row['Value'].">."$row['Name']."</option>"; //Add an option for every row in the DB
}
echo "</select>";
?>-
drcolossus
- Forum Newbie
- Posts: 5
- Joined: Tue Mar 23, 2004 8:19 pm
- Location: Japan
thanks Deemo! So far it's understandable! I just wonder how it's possible to have the selection (which the user makes on the dropdown menue) can be saved back into the mysql database and if the selected menue item can be displayed as a default when the data is read in again. usually the first item in the dropdown menue is always shown by default but if i saved for example menue item 3, can this item be shown by default when fetching the data into the form again?
thanks for all help!
thanks for all help!
Add a little tweak...
Assuming the data containing the selection menu is $_POST['selection']:-
Assuming the data containing the selection menu is $_POST['selection']:-
Code: Select all
<?php
echo "<select name = 'Options'>"; //Start selection box
$query = "SELECT * FROM Table"; //Get Data from DB
$result = mysql_query($query) or die("Couldn't Execute Query ".mysql_error());
while ($row = mysql_fetch_array($result))
{
if($row == $_POST['selection']) {
echo "<option value = ".$row['Value']." selected>."$row['Name']."</option>";
} else {
echo "<option value = ".$row['Value'].">."$row['Name']."</option>"; //Add an option for every row in the DB
}
}
echo "</select>";
?>