Say I have a form that contains a drop-down list populated by values stored in my database:
<form action="" method="POST">
<select name="item_select">
<?php
$query = "SELECT * FROM my_table ORDER BY name";
$result = mysql_query($query);
while ($row = mysql_fetch_object($result)) {
?>
<option value=<?php echo $row->id; ?> > <?php echo $row->name; ?></option>
<?php }// end while?>
</select>
<br /><br />
<input name="action_1" type="submit" value="Action 1" />
<input name="action_2" type="submit" value="Action 2" />
</form>
This all works fine, but I have a minor aesthetic issue I’d like to see if I can fix. Specifically every time I submit the form, the drop-down list selects the first item in the list, no matter which one I select. For instance.
List:
Item1 (selected)
Item2
I selected Item2 and submit the form, after the page submits it is still
List:
Item1 (selected)
Item2
Is there a way to have the drop-down “remember” which item was selected? For instance the desired effect after submitting the form for Item2 should be.
List:
Item1
Item2 (selected)
Just compare the submited value with the current one in for loop: