chien pin wang
chien pin wang

Reputation: 567

jquery remember option selected

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>

Upvotes: 0

Views: 1306

Answers (2)

asprin
asprin

Reputation: 9823

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.

Upvotes: 0

Pankucins
Pankucins

Reputation: 1720

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.

Upvotes: 2

Related Questions