Forum Moderators: coopster

Message Too Old, No Replies

PHP for form checkbox persistence

         

Artie_J

5:07 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



Hi all. I'm new to programming, and I was looking on the web for handling form checkboxes, specifically keeping them checked when either passing the name as a variable to another page, or when refreshing a page.
I reasoned out a simple method that I didn't see posted anywhere else, so I thought I'd offer it for review.

<?php
if($chkbox=="on"){
$chkswitch="checked='checked' value='on'";
}
?>

<form method="post" action="<? echo $PHP_SELF;?>" name="anything">
<input type="checkbox" name="chkbox" <? echo $chkswitch;?>>
<input type="submit" value="Submit" name="submit">
</form>

I'm using this sending and receiving "chkbox" from a MYSQL database also. Everthing works great!

jd01

7:33 pm on Aug 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Artie_J,

Welcome to WebmasterWorld.

<input type="checkbox" name="chkbox" <? echo $chkswitch;?>>

I use:

<input type="checkbox" name="chkbox" value="on" <? if($chkbox==="on") { echo "checked"; } ?>>

It just keeps things that effect the checkbox all in one place, so if I want to make adjustments I can find it easily.

Justin

Artie_J

1:16 am on Aug 3, 2005 (gmt 0)

10+ Year Member



Ah good! Thanks.
But...
where the hell were you when I was trying to figure this out? hahaha!

jd01

7:52 pm on Aug 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should have joined sooner!

Come back and see us again.

Justin