Log in

View Full Version : using foreach for unchecked boxes



james438
02-24-2008, 05:31 AM
How can you use foreach() and $_POST to retrieve and display all of the information including values from checkboxes where the checkboxes were not checked?

For example
$a=0;
foreach ($_POST as $field[] => $value[])
{echo "$field[$a] = $value[$a]<br>";$a++;}will display all of the values except for the values from checkboxes that were not checked.

any ideas?

Twey
02-24-2008, 10:05 AM
You can't -- unchecked checkbox values are not sent by the browser.

magik
02-24-2008, 10:40 AM
You could assume that all boxes are unchecked unless checked though.
?

djr33
02-24-2008, 11:35 AM
Yeah, certainly, if there is no value it's checked, but that won't let you know what the names of the fields were, which is why looping through them would help. So, not sure what to suggest here. Best just write out an array of the field names and remove those from the array that ARE checked, then you have a list of those that are not.

james438
02-24-2008, 04:09 PM
Thanks guys. One idea I came up with was to put all of the fields into an array and then loop through the array similar to what djr33 was suggesting. If that makes sense.

Leafy
02-26-2008, 12:34 AM
Wouldn't it be easier instead of un-checking things you DON'T want, to check things that you do? Unless you're doing some type of signup form or something.