Reply to Re: How do get the state of checkboxes in php?

Your name:

Reply:


Posted by Kelsey Sigurdur on 09/08/07 01:00

On Fri, 07 Sep 2007 22:17:13 +0000, Tom.Slayton wrote:

> On Sep 7, 5:02 pm, Kelsey Sigurdur <kel...@m-f-d.org> wrote:
>> On Fri, 07 Sep 2007 13:06:34 -0500, Lorenzo Thurman wrote:
>> > I have a group of checkboxes drawn by javascript and I need to get
>> > each of their states into PHP after submitting the page. I should
>> > think I could get them in a POST, but that fails. Anyone have any
>> > ideas? TIA
>>
>> Use an additional hidden input, with the same name as the checkbox and
>> a value of 0. In almost all cases you'll want the hidden input to
>> appear in the source before the actual checkbox.
>>
>> When the form is submitted, if the checkbox has been checked, it's
>> value will override the hidden input of the same name. If it hasn't
>> been checked the hidden input (with its value of 0) will be in the
>> $_POST array.
>>
>> AFAIK this is the only way to do it as unchecked checkboxes are not
>> passed to POST at all.
>>
>> --
>> Kelsey Sigurdur
>
>
>
> You could just test for the value in the post buffer and set a variable:
> $lnChecked = ($_POST['CheckboxName'] == "on") ? 1 : 0 ;
>
> Best,
>
> Tom.

Assuming you know the field names beforehand, certainly. However, in
dynamic cases where, for example, the form elements are drawn from and map
to columns in a table, it's often simpler to generate an additional input
rather than code for the existence of all possible fields.

I actually came across this issue for the first time just a few days ago
while coding up the configuration variables for a simple gallery
application.

I had the choice between dynamically building an SQL query from the
variables available in the POST array or simply ensuring that all the
required fields/columns are represented in the POST array.

The latter requires minimal extra logic, only the addition of hidden
fields for the boolean types (checkboxes) coming out of the DB, while the
former would require all kinds of if/else logic to generate the SQL
necessary to update the table.

FWIW, I scored the hidden input tip from this O'Reilly article:
http://www.onlamp.com/pub/a/php/2003/03/13/php_foundations.html

***
"Working with Checkboxes in PHP

Due to the nature of checkboxes, you may have already realized that
checkboxes will not show up in PHP unless they have been checked. Dealing
with this circumstance can be annoying (and perhaps insecure) unless
properly dealt with. The first of these methods basically involves
creating a hidden form element with the same name as the checkbox element
and setting its value to zero, as shown below:

<input type="hidden" name="mycheckbox" value="0">
<input type="checkbox" name="mycheckbox" value="1">

The idea behind this solution is based on how PHP will respond if two
elements (using the same method) have the same name. As was the case with
the <select> element when in multi-select mode, PHP uses the last value
provided to it. The hidden element will be used because the checkbox
element is not sent to PHP if it is unchecked. Likewise, if the checkbox
element is checked when the form is submitted, its value will override
that of the hidden element.

Unfortunately, the drawback to this solution is a complete reliance on the
browser to always send the hidden element before the checkbox element.
Although it is logical to assume that elements will be sent in the same
order as they are presented in the HTML document, there is no guarantee
that this situation will be the case.

The second solution to this problem is much more eloquent for most
situations. Unless you are unsure if a given checkbox was displayed to the
user (if it may or may not be present in the form, depending the logic
that created the form) create a boolean value based on the existence of
the checkbox:

<?php
/* Assume a checkbox by the name 'mycheckbox' exists */
$checkbox = ($_GET['mycheckbox'] == 1);
?>

The value of the $checkbox boolean will be set to true if the mycheckbox
element exists (with the proper value), and false otherwise."
***

--
Kelsey Sigurdur

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация