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

Posted by Tony Marston on 09/08/07 21:47

"thomasriise" <thomasriise@gmail.com> wrote in message
news:1189168235.964109.89480@57g2000hsv.googlegroups.com...
> Hi Guys,
>
> I have this code:
>
>
> ---
> <?php
>
> echo '<html><head><title>checkbox tester</title></head>';
>
> if ($submit){
>
> foreach (($fruit = $_REQUEST["fruit"]) as $fruitid ) {
>
> if ($fruitid == "notcheched") {
> echo 'This fruit was <b>NOT</b> checked: '.$fruitid.'<br />';
> } else {
> echo 'This fruit WAS checked: '.$fruitid.'<br />';
> }
> }
> echo '-------------------------------------<br />';
> }
>
> echo '
> <form method="post" action="checker.php">
> Choose a fruit:<br><br>
> <input type="checkbox" name="fruit[]" value="apples">apples <br>
> <input type="checkbox" name="fruit[]" value="oranges">oranges <br>
> <input type="checkbox" name="fruit[]" value="peaches">peaches <br>
> <input type="checkbox" name="fruit[]" value="mangos">mangos<br>
> <input type="submit" name="submit">
> ';
>
> if (empty($_REQUEST["fruit"])){
> $fruit = 'notchecked';
> }
>
> echo '</form></body><html>';
> ---
>
> 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

 

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

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