How to keep the values of dropdown list when the form is submitted in php?

html, php

Solution

It will be fast and better if you use below :-

<select name="gender">
<option value="Male" <?php echo (isset($_POST['gender'] && $_POST['gender'] == 'Male')?'selected="selected"':''; ?> >Male</option>
<option value="Female" <?php echo (isset($_POST['gender'] && $_POST['gender'] == 'Female')?'selected="selected"':''; ?> >Female</option>
<option value="Other" <?php echo (isset($_POST['gender'] && $_POST['gender'] == 'other')?'selected="selected"':''; ?> >Other</option>
</select>

Problem

Here is the code i have written, Please do help me to solve this issue. ``` <select name="gender" id="gender"> <option value="M"<?php if(isset($_POST['gender'])=="M"){echo "selected='selected'";}?>>Male</option> <option value="F"<?php if(isset($_POST['gender'])=="F"){echo "selected='selected'";}?>>Female</option> <option value="O"<?php if(isset($_POST['gender'])=="O"){echo "selected='selected'";}?>>Other</option> </select> ``` When i submit the form by selecting Male in dropdown list, it should keep the selected value till i change that in the next submission of form. In the above code if i select the Male and submit the form, its showing me Others. so please need a little help. Thanks in advance to those who would like to fix the issue.

Original source