0

How to use jquery remember the option which user has previously selected when the from post to itself? (selected="selected")

<form action="abc.php" method="POST">
<select id="dropdown">
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
</select>
<input type="submit" />
</form>
chien pin wang
  • 559
  • 1
  • 4
  • 15

2 Answers2

2

If you really want to do it with JavaScript you'll have to use cookies. You can read about that here. There are also cookie management plugins for jQuery, just google something along the lines of jQuery cookie plugin.

Otherwise, if you're returning to the form because some fields are not filled or a similar problem I suggest using PHP (as I see you're using it) to set the selected values.

Pankucins
  • 1,690
  • 1
  • 16
  • 25
0

If I understood your requirement correctly:

<form action="abc.php" method="POST">
<select id="dropdown" name="dropdown">
    <option <?php if(isset($_POST['dropdown']) && $_POST['dropdown'] == 'A') echo 'selected="selected"';?> value="A">A</option>
    <option <?php if(isset($_POST['dropdown']) && $_POST['dropdown'] == 'B') echo 'selected="selected"';?> value="B">B</option>
    <option <?php if(isset($_POST['dropdown']) && $_POST['dropdown'] == 'C') echo 'selected="selected"';?> value="C">C</option>
</select>
<input type="submit" />
</form>

PS : You'll also need a name attribute for the select tag if you're using PHP to submit the form. Also I'm assuming abc.php is the same page in which this form is present.

asprin
  • 9,579
  • 12
  • 66
  • 119