'drop down list keep last selected option

I have created a profile page in php where a user using an html drop down list chooses gender. The html code is the following:

Gender<select name="gender"> 
    <option value=" "> EMPTY </option> 
    <option value="Male">Male</option> 
    <option value="Female">Female</option> 
</select> 

After the user chooses gender the form dispatches and saves the data into database. The problem is that if the user visits again the profile page the drop down list does not keep the value that the user selected before, but shows the first option value each time. How can I modify this so that the drop down list will show the selected value that users enters before?



Solution 1:[1]

Set the "selected" attrubute on the correct option tag. You'll have to figure that out either on the server before you deliver the HTML or use Javascript client-side, depending on where you're storing the user's selection.

Solution 2:[2]

Try like below... it will help you ...

<option value=" " selected> EMPTY </option> 

If we set Selected attribute in Option tag ... then it specifies that an option should be pre-selected when the page loads

Solution 3:[3]

after selecting and form is submited then the selected value must be saved in a session variable . and when the person visits page again set the selected attribute to the corresponding by checking the session value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nathan Wallace
Solution 2 Pandian
Solution 3 Pannan Mp