Re: checkbox unchecked

From: Date: Sun, 02 Dec 2007 21:08:35 +0000
Subject: Re: checkbox unchecked
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-265389@lists.php.net to get a copy of this message
At 12:36 PM -0600 12/2/07, Larry Garfield wrote:
First of all, using "y" and "n" for boolean values (such as a checkbox) is very sloppy. "n" is boolean True. A boolean value should evaluate correctly in a boolean context. For that, you should use 1 and 0 for your values. What I usually do is this: <input type="hidden" name="foo" value="0" /> <input type="checkbox" name="foo" value="1" <?php echo $checked; ?> /> Then when it gets submitted, foo will get the value of the form element that was submitted last that has a value. That is, if the checkbox is checked then foo will be 1, otherwise it will be 0. That gives you a nice, clean boolean value you can rely on being present (mostly <g>).
Larry: Not that you said otherwise, but if the programmer does not set the value for a checkbox, html will provide values -- that may lead to confusion for newer programmers. See here: http://webbytedd.com/bbbb/checkbox/ If you will note, without specifically setting the value, html will return "on". Also, I'm sure it's an oversight, but your code above should be: <?php if ($foo) echo('checked'); ?> Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com

« previous php.general (#265389) next »