How do get the state of checkboxes in php?

How do get the state of checkboxes in php?

am 07.09.2007 20:06:34 von Lorenzo Thurman

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

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

am 07.09.2007 22:05:05 von Jerry Stuckle

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

It's how HTML works. Only checked boxes are sent in the GET or POST
request. Unchecked ones are not.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 07.09.2007 23:02:50 von Kelsey Sigurdur

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

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

am 08.09.2007 00:17:13 von Tom.Slayton

On Sep 7, 5:02 pm, Kelsey Sigurdur 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.

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

am 08.09.2007 01:38:28 von Courtney

Kelsey Sigurdur 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.
>

This is the generic way to do javascript/php dialogue.

EITHER set your submit button to invoke javascript and cahin to the next
page using get variables.. i.e. replace.window("myurl?myvar=Myvalue");

OR set up a POST form with id hidden variables, do a
getElementbyId('myvar').value="Myvalue";
and then submit()....

Syntax NOT guaranteed ;-)

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

am 08.09.2007 02:38:11 von Macca

The HTML form:

Please select every sport that you play.



Soccer:
>
Football: >

Baseball: >

Basketball: value="basketball" />





------------------------------------------------------------ ---------------------------

The php script:


$sports = $_POST['sports']; // is an array holding all the checked
boxes

foreach ($sports as $checked_sport){

echo $checked_sport."
"; // Output the checked boxes to the
output device.

}

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

am 08.09.2007 03:00:48 von Kelsey Sigurdur

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

> On Sep 7, 5:02 pm, Kelsey Sigurdur 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.h tml

***
"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:




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

.....



And so on

The names of the checkboxes will now be in the array $_POST['cbname']
and you can check it with something like:

foreach ($_POST['cbname'] as $cbname) {
if (isset($_POST[$cbname]))
echo "Checkbox $cbname is checked
\n";
else
echo "Checkbox $cbname is NOT checked
\n";
}

This is independent of the order in which the browser sends the form data.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 09.09.2007 01:33:17 von Kelsey Sigurdur

On Fri, 07 Sep 2007 21:23:29 -0400, Jerry Stuckle wrote:

> Kelsey Sigurdur 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.
>>
>
> Not at all the only way. Your method depends on the order the browser
> sends post data. Sure, they usually send the input fields in the order
> the fields appear in the form, but this is not at all required. A
> change to the browser will break your pages.
>
> A better way is to have a hidden field with the name of the checkbox, i.e.
>
>
>
> ....
>
>
>
> And so on
>
> The names of the checkboxes will now be in the array $_POST['cbname']
> and you can check it with something like:
>
> foreach ($_POST['cbname'] as $cbname) {
> if (isset($_POST[$cbname]))
> echo "Checkbox $cbname is checked
\n";
> else
> echo "Checkbox $cbname is NOT checked
\n";
> }
>
> This is independent of the order in which the browser sends the form data.

Excellent solution. Thanks!

--
Kelsey Sigurdur

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

am 09.09.2007 04:40:31 von Larry Anderson

Keep in mind if no boxes are checked you don't get the value passed
back. Following are a couple functions I put together to retain my
sanity in checklists and other array input use it like:

$arrayname = postarrayfix('postarrayname');

It strips out any potentially bad HTML and slashes if magic quotes are
on.


/*$_POST Fixer, if the following lists are empty, the POSTs return
nulls, which is bad, as the code needs arrays (even empty) to display,
this fixes it.*/

function postArrayFix($postname){
if( empty($_POST[$postname]) ){
$value = array();
} else {
$value = cleanArray($_POST[$postname]);
}
return $value;
}

/*This one is for if you are reading in arrays from $_POST data, it
will walk deep into the array and clean it up.
*/

function cleanArray($value)
{
if(get_magic_quotes_gpc()) {
$value = is_array($value) ?
array_map('cleanArray', $value) :
stripslashes(strip_tags($value));
} else {
$value = is_array($value) ?
array_map('cleanArray', $value) :
strip_tags($value);
}
return $value;
}

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

am 10.09.2007 12:04:18 von unknown

Post removed (X-No-Archive: yes)

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

am 10.09.2007 14:00:16 von Jerry Stuckle

David Gillen wrote:
> Jerry Stuckle said:
>> 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
>> It's how HTML works. Only checked boxes are sent in the GET or POST
>> request. Unchecked ones are not.
>>
> So check isset($_POST['myCheckBox']).
>
> D.

Yep, that will work just fine.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================