Reputation: 4740
I want to insert a value from the database into my dropdown menu in php. Here's what I'm looking to do: I have an edit form page which pulls info from the database and populates itself. However, the dropdown menu is hardcoded and I want the value from the database to be the one that is selected when that pages loads, this allows the user to keep that value or pulldown the menu and select something else.
Is there any way of doing this? I don't know if I'm making any sense at all :)
<html>
<head>
</head>
<body>
<select>
<option>Select employ</option>
<?php
mysql_connect('localhost','user','pass');
mysql_select_db('employ');
$query="select id, name from employ order by name asc";
$result=mysql_query($query);
while(list($id, $name)=mysql_fetch_row($result)) {
echo "<option value=\"".$id."\">".$name."</option>";
}
?>
</select>
</body>
</html>
That code is for reading and dynamically creating a dropdown menu, but that is not what I'm looking for. Instead, the dropdown has already been created on the client side before the page loads - so I want just the 'name' that is returned from the database to be selected in the dropdown menu just like other values are displayed in the form.
Any help would be appreciated.
Upvotes: 1
Views: 8305
Reputation: 685
I would use a code like this:
<?php
$host="localhost";
$username="user";
$password="password";
$db_name="employ";
$tbl_name="employ";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY name ASC";
$result=mysql_query($sql);
if ($result === false) { echo "An error occurred."; }
?>
<html>
<head>
<title>Your title here</title>
</head>
<body>
<select name="usrname" id="usrname">
<option>Select employ</option>
<?php
while($rows=mysql_fetch_array($result)){
?>
<option value="<? echo $rows['id']; ?>"><? echo $rows['name']; ?></option>
<?php
}
mysql_close();
?>
</select>
</body>
</html>
It sets the value of the selected item as your ID and shows the name just as you wanted it to :-) Hope it helped
Upvotes: 2
Reputation: 4740
Thanks folks, I finally got an answer somewhere on this site. It was an answer to someone else's question and it work for me too.
Upvotes: 0
Reputation: 182
Mutch cleaner example:
while(list($id, $name)=mysql_fetch_row($result)) {
echo '<option value='.$id.'" '.($selected==$name? 'selected="selected"':'').'>'.$name.'</option>';
}
Upvotes: 1
Reputation: 4836
I'm not sure I've got the right idea here, but I'll get it a crack.
I'm assuming you want the currently selected employee to be the selected employee?
I don't know how you plan on tracking which user is selected, but here I'll assume you have it in a variable $selected
.
while(list($id, $name)=mysql_fetch_row($result)) {
if($selected==$name) {
echo "<option value=\"".$id."\" selected=\"selected\">".$name."</option>";
} else {
echo "<option value=\"".$id."\">".$name."</option>";
}
}
Hope I have the right idea, and that this helps :)
Upvotes: 1
Reputation: 4421
If I understand this right, you want to "repopulate the form". To do that, try this:
$currentValue = isset($_REQUEST['selectName']) ? $_REQUEST['selectName'] : null;
while(list($id, $name)=mysql_fetch_row($result)) {
echo "<option value=\"{$id}\";
if ($id == $currentValue) echo ' selected="selected"';
echo ">".$name."</option>";
}
Basically, you get the current value if it's available from the post data, and then mark the selected element selected it if it's there.
Upvotes: 0