Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

php

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.

like image 236
Preetham Rao Avatar asked Dec 20 '22 21:12

Preetham Rao


1 Answers

Assuming you have this :

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

Then in your submitted page, place that :

<select name="gender">
  <option value="Male" <?php if ($_POST['gender'] == 'Male') echo 'selected="selected"'; ?> >Male</option>
  <option value="Female" <?php if ($_POST['gender'] == 'Female') echo 'selected="selected"'; ?> >Female</option>
  <option value="Other" <?php if ($_POST['gender'] == 'Other') echo 'selected="selected"'; ?> >Other</option>
</select>
like image 74
Guillaume Lehezee Avatar answered Dec 24 '22 02:12

Guillaume Lehezee