You are here: Re: parse empty checkbox to php « PHP Programming Language « IT news, forums, messages
Re: parse empty checkbox to php

Posted by Rik Wasmus on 09/07/07 12:39

On Fri, 07 Sep 2007 14:30:35 +0200, thomasriise <thomasriise@gmail.com> =
=

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

Use $_POST.

>
> if ($fruitid =3D=3D "notcheched") {
> echo 'This fruit was <b>NOT</b> checked: '.$fruitid.'<br />';
> } else {
> echo 'This fruit WAS checked: '.$fruitid.'<br />';
> }
> }
> echo '-------------------------------------<br />';
> }
>
> echo '
> <form method=3D"post" action=3D"checker.php">
> Choose a fruit:<br><br>
> <input type=3D"checkbox" name=3D"fruit[]" value=3D"apples">apples <br>=

> <input type=3D"checkbox" name=3D"fruit[]" value=3D"oranges">oranges <b=
r>
> <input type=3D"checkbox" name=3D"fruit[]" value=3D"peaches">peaches <b=
r>
> <input type=3D"checkbox" name=3D"fruit[]" value=3D"mangos">mangos<br>
> <input type=3D"submit" name=3D"submit">
> ';
>
> if (empty($_REQUEST["fruit"])){
> $fruit =3D 'notchecked';
> }
>
> echo '</form></body><html>';
> ---
>
> 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:<br><br>
<input type=3D"hidden" name=3D"fruitkeys[]" value=3D"apples"><input =

type=3D"checkbox" name=3D"fruit[apples]" value=3D"apples">apples<br>
<input type=3D"hidden" name=3D"fruitkeys[]" value=3D"oranges"><input =

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

<input type=3D"hidden" name=3D"fruitkeys[]" value=3D"peaches"><input =

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

<input type=3D"hidden" name=3D"fruitkeys[]" value=3D"mangos"><input =

type=3D"checkbox" name=3D"fruit[mangos]" value=3D"mangos">mangos<br>
<input type=3D"submit" name=3D"submit">

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

NOT choose $fruit<br>";
}

-- =

Rik Wasmus

 

Navigation:

[Reply to this 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

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