0
<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?

Neof
  • 13
  • 3

2 Answers2

2

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>
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
0
    <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.

Avinash Raut
  • 1,872
  • 20
  • 26