Checkbox and list problem

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
jack2010
Forum Newbie
Posts: 1
Joined: Sun Feb 07, 2010 8:06 pm

Checkbox and list problem

Post by jack2010 »

Hi there

I want my form to keep values entered by user after submit form. It works for input text fields but for lists and checkbox it refreshes the selection. I have no idea how lists can rember their selected option.

And this is what i'm doing for Checkbox

if (isset($_POST['cb1']))
{$_SESSION['cb1']= 'Yes';}

<input type="checkbox" name="cb1" value= "<?php echo $_SESSION['cb1']; ?>" onClick="validate();" >

Any help ??
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Checkbox and list problem

Post by manohoo »

Jack, for lists the value entered should be in the form:
<OPTION value="x" DEFAULT>$enteredValue</OPTION>
"default" is the key word

for check boxes:
<INPUT TYPE="CHECKBOX" NAME="sendmail" VALUE="send" CHECKED>
"checked" is the key word
limitdesigns
Forum Commoner
Posts: 25
Joined: Sat Feb 06, 2010 9:05 pm

Re: Checkbox and list problem

Post by limitdesigns »

To expand on that, you would have a php conditional to determine whether you add "checked='checked'" (this is the more valid option, rather than simply CHECKED) to the input tag. Something like...

Code: Select all

 
<input type="checkbox" <?php if(isset($_POST['cb1'])) { echo "checked='checked'";}?> name="cb1" />
 
Post Reply