parse empty checkbox to php

parse empty checkbox to php

am 07.09.2007 14:30:35 von thomasriise

Hi Guys,

I have this code:


---

echo 'checkbox tester';

if ($submit){

foreach (($fruit = $_REQUEST["fruit"]) as $fruitid ) {

if ($fruitid == "notcheched") {
echo 'This fruit was NOT checked: '.$fruitid.'
';
} else {
echo 'This fruit WAS checked: '.$fruitid.'
';
}
}
echo '-------------------------------------
';
}

echo '


Choose a fruit:


apples

oranges

peaches

mangos


';

if (empty($_REQUEST["fruit"])){
$fruit = 'notchecked';
}

echo '
';
---

Only the checked boxes gets parsed to php - how do I get the UNchecked
boxes?

Thanks!

Re: parse empty checkbox to php

am 07.09.2007 14:39:58 von luiheidsgoeroe

On Fri, 07 Sep 2007 14:30:35 +0200, thomasriise =
=

wrote:
> I have this code:
>
>
> ---
> >
> echo 'checkbox tester';
>
> if ($submit){
>
> foreach (($fruit =3D $_REQUEST["fruit"]) as $fruitid ) {

Use $_POST.

>
> if ($fruitid == "notcheched") {
> echo 'This fruit was NOT checked: '.$fruitid.'
';
> } else {
> echo 'This fruit WAS checked: '.$fruitid.'
';
> }
> }
> echo '-------------------------------------
';
> }
>
> echo '
>


> Choose a fruit:


> apples
=

> oranges r>
> peaches r>
> mangos

>
> ';
>
> if (empty($_REQUEST["fruit"])){
> $fruit =3D 'notchecked';
> }
>
> echo '';
> ---
>
> Only the checked boxes gets parsed to php - how do I get the UNchecked=

> boxes?

You don't, it's a limitation by HTML. If you want to check which ones ar=
e =

checked and which ones aren't, give them a name (or an index in an array=
=

by 'name=3D"fruit[apple]"', and check with isset. If you don't know for =
=

which ones to check in the receiving script, either store that in a =

session on creating the form, or add a hidden input holding the values:

Choose a fruit:



type=3D"checkbox" name=3D"fruit[apples]" value=3D"apples">apples


type=3D"checkbox" name=3D"fruit[oranges]" value=3D"oranges">oranges
=


type=3D"checkbox" name=3D"fruit[peaches]" value=3D"peaches">peaches
=


type=3D"checkbox" name=3D"fruit[mangos]" value=3D"mangos">mangos



receiving script:
foreach($_POST['fruitkeys'] as $fruit){
echo isset($_POST['fruit'][$fruit]) ? "you choose $fruit
" : "you di=
d =

NOT choose $fruit
";
}

-- =

Rik Wasmus

Re: parse empty checkbox to php

am 07.09.2007 15:05:16 von thomasriise

When I have this code you postet, nothing happens:

--

echo 'checkbox tester';

if ($submit){
foreach($_POST['fruitkeys'] as $fruit){
echo isset($_POST['fruit'][$fruit]) ? "you choose
$fruit
" : "you did NOT choose $fruit
";
}
}

echo '
Choose a fruit:


type="checkbox" name="fruit[apples]" value="apples">apples

type="checkbox" name="fruit[oranges]" value="oranges">oranges

type="checkbox" name="fruit[peaches]" value="peaches">peaches

type="checkbox" name="fruit[mangos]" value="mangos">mangos


';

echo '';

?>
--

What am I doing wrong?

Re: parse empty checkbox to php

am 07.09.2007 15:22:11 von luiheidsgoeroe

On Fri, 07 Sep 2007 15:05:16 +0200, thomasriise =
=

wrote:

> When I have this code you postet, nothing happens:
>
> --
> >
> echo 'checkbox tester';
>
> if ($submit){

if(isset($_POST['submit'])){

check why register_globals should be off.

> foreach($_POST['fruitkeys'] as $fruit){
> echo isset($_POST['fruit'][$fruit]) ? "you choose
> $fruit
" : "you did NOT choose $fruit
";
> }
> }

Starting form tag?

> echo '
> Choose a fruit:


> > type=3D"checkbox" name=3D"fruit[apples]" value=3D"apples">apples

> > type=3D"checkbox" name=3D"fruit[oranges]" value=3D"oranges">oranges r>
> > type=3D"checkbox" name=3D"fruit[peaches]" value=3D"peaches">peaches r>
> > type=3D"checkbox" name=3D"fruit[mangos]" value=3D"mangos">mangos

>
> ';
>
> echo '';
>
> ?>
-- =

Rik Wasmus

Re: parse empty checkbox to php

am 08.09.2007 23:47:34 von Tony Marston

"thomasriise" wrote in message
news:1189168235.964109.89480@57g2000hsv.googlegroups.com...
> Hi Guys,
>
> I have this code:
>
>
> ---
> >
> echo 'checkbox tester';
>
> if ($submit){
>
> foreach (($fruit = $_REQUEST["fruit"]) as $fruitid ) {
>
> if ($fruitid == "notcheched") {
> echo 'This fruit was NOT checked: '.$fruitid.'
';
> } else {
> echo 'This fruit WAS checked: '.$fruitid.'
';
> }
> }
> echo '-------------------------------------
';
> }
>
> echo '
>


> Choose a fruit:


> apples

> oranges

> peaches

> mangos

>
> ';
>
> if (empty($_REQUEST["fruit"])){
> $fruit = 'notchecked';
> }
>
> echo '
';
> ---
>
> Only the checked boxes gets parsed to php - how do I get the UNchecked
> boxes?
>
> Thanks!

You don't. This is an HTML problem, not a PHP problem. A checkbox is only
included in the POST array when it is checked ON.

However, there is a workaround. Simply define a hidden field with the same
name as the checkbox immediately BEFORE the real checkbox, and assign it a
value of FALSE or OFF. Only one field with the same name will appear in the
POST array, so if the checkbox is not ON it will allow the hidden FALSE
value to pass through. If the checkbox is ON, then it will replace the value
from the hidden field.

--
Tony Marston

http://www.tonymarston.net
http://www.radicore.org