Reputation: 13
<form method="post" id="employee" action="change.php">
<select id="employeename" name="employeename" onchange="this.form.submit()">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
?>
</select>
</form>
The form will be submitted when select value is changed,
Once the form is submitted, it will redirect to the same page. How do I set the selected option value with the one I just selected?
Upvotes: 0
Views: 2273
Reputation: 2113
<form method="post" id="employee" action="change.php">
<select id="employeename" name="employeename" onchange="this.form.submit()">
<?php
while ($row = mysql_fetch_array($result))
{
if($_POST['employeename']==$row['name'])
echo "<option value='".$row['name']."' selected>".$row['name']."</option>";
else
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
?>
</select>
</form>
just marked as selected if you find selected key in available list.
Upvotes: 0
Reputation: 16963
Change your form, especially the <select>
dropdown list in the following way,
<form method="post" id="employee" action="change.php">
<select id="employeename" name="employeename" onchange="this.form.submit()">
<?php
while ($row = mysql_fetch_array($result)){
$output = "<option value='".$row['name']."'";
if($_POST['employeename'] == $row['name']){
$output .= " selected='selected'";
}
$output .= ">".$row['name']."</option>";
echo $output;
}
?>
</select>
</form>
Upvotes: 2