how to retain list value on form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
om.bitsian
Forum Commoner
Posts: 36
Joined: Wed Sep 09, 2009 4:13 am

how to retain list value on form

Post by om.bitsian »

Hi friends.....
i have some select box in my form like
<label>1. Sex</label>
<select name="sx">
<option value='M'>Male</option>
<option value='F' >Female</option>
</select>
<br><br>

when i select Female and suppose there is an error in Name field , at that time when page reloded it will again reset the selected value i.e Male will appear in form...
for name field i am using value="<?php if (isset($_POST['submit'])) echo $_POST['name']; ?>" in the input labe ...in same way is there any option for select box

thank you
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: how to retain list value on form

Post by pbs »

Use select box like this.

Code: Select all

 
<select name="sx">
<option value='M' <?php if (trim($_POST["sx"]) == "M") echo " selected ";?> >Male</option>
<option value='F' <?php if (trim($_POST["sx"]) == "F") echo " selected ";?> >Female</option>
</select>
 
om.bitsian
Forum Commoner
Posts: 36
Joined: Wed Sep 09, 2009 4:13 am

Re: how to retain list value on form

Post by om.bitsian »

thank you pbs .......its giving warning
Notice: undifined index :sx
Post Reply