Reputation: 69
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.
Upvotes: 4
Views: 19991
Reputation: 2956
instead of
<option value="M"<?php
if(isset($_POST['gender'])=="M")
--------^^^^^^^^^^^^
{echo "selected='selected'";}?>>Male</option>
try
<option value="M"<?php if((!empty($_POST['gender']) && $_POST['gender']=="M")
------^^^^^^^^
{echo "selected='selected'";}?>>Male</option>
Upvotes: 0
Reputation: 1171
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>
Upvotes: 5
Reputation: 904
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>
Upvotes: 6