Reputation: 1
I have a requirement where the user will fill out and submit a form. If there are any errors found on the server side, the same form will be displayed to the user, but now the previously selected value of the drop down is not selected by default again. I want the default selected value of the drop down to be based the value the user selected before.
Upvotes: 0
Views: 197
Reputation: 39
which server side language you are using? this is not looking the JavaScript problem, because after submitting the page get refreshed and you are validating on server side, so you can use POST Global vars to return the value Or you can ask me with more information
Upvotes: 0
Reputation: 5620
To ensure that the default selected value is always the previously selected one, you could catch the selected value on the server-side script that receives the form. Then, save this value to a session variable and compare it later with this (inside the loop that creates the dropdown) :
if (isset($_SESSION["previousValue"])) {
if ($value == $_SESSION["previousValue"]) {
echo '<option value="$value" selected="selected">$value</option>';
} else {
echo '<option value="$value">$value</option>';
}
}
Hope this helps you
Upvotes: 0
Reputation:
You're going to some how have to maintain state between page refreshes.
For example, you could store the value in a cookie while the user is filling out the form, and when the page refreshes (with errors) you can check for the value and deal with it accordingly.
Upvotes: 0
Reputation: 667
The code which is creating the drop down on the server should check if while printing options if the option matches the user chosen option, to mark is "selected"
Upvotes: 1