Reply to Re: - setting checkboxes

Your name:

Reply:


Posted by Jerry Stuckle on 08/08/06 00:15

Sonnich wrote:
> Jonathan N. Little wrote:
>
>>Sonnich wrote:
>>
>>[You are improperly snipping who wrote what quote which can get quite
>>confusing, try to keep or replace quoted author notices which I have
>>restored to show you....]
>>
>>
>> >> Sonnich wrote:
>> >>>
>>
>>>>>I found that it should work here too:
>>>>>
>>>>> if( !(isset($_POST[$part1[$i]]) && ($_POST[$part1[$i]] ==
>>>>>"")) )
>>>>> echo " checked";
>>>>> else
>>>>> echo "";
>>>>>
>>>>>Idea: checkbox is default checked, but only if set and set to nothing
>>>>>it is not set.
>>>>>Boolean algebra. Only if set, and set to nothing, then it is not set.
>>>>>
>>>>>But by some reason it does not work. I have done this a 147533 times,
>>>>>and here it does not work. Why?
>>>>>
>>
>> > Jonathan wrote:
>> >>
>>
>>>>I think a misplaced closing parentheses:
>>>>
>>>
>>>You were right. This is what I have now:
>>>
>>> if( !(isset($_POST[$part1[$i]])) && ($_POST[$part1[$i]] ==
>>>"") )
>>> echo " checked";
>>> else
>>> echo "";
>>>
>>>but is still does not work. It sets them first time as it should, but
>>>after that it inverts the selection.
>>>I dont get it.
>>>
>>>Any ideas anyone?
>>>
>>
>>Not enough info to determine, need URL and your source code. What you
>>you me by 'sets them'? Sets what? And 'after the first time'? Are your
>>reposting to the same script?
>
>
> Yes, I am reposting to the same script. An example:
>
>
> <html>
> <form method=post name='myform' action='sjjtest.php'><!-- same file -->
> <table>
> <?
> $part1[]="a";
> $part1[]="b";
> $part1[]="c";
> for($i=0; $i<count($part1); $i++)
> {
> echo "<td><input name=\"".$part1[$i]."\" type=\"checkbox\"
> value=\"true\"";
> if( !(isset($_POST[$part1[$i]])) && ($_POST[$part1[$i]] == "") )
> echo " checked";
> echo ">". isset($_POST[$part1[$i]])."-".$_POST[$part1[$i]]
> ."</td></tr>";
> }
> ?>
> <tr><td><input class="box" name="apply_selection" type="submit" value="
> Apply selection "></td></tr>
> </table>
> </form>
> </html>
>
> Try it and you will see
>

If you had all errors enabled and were displaying them, you would have
found errors in both statements with isset():

Notice: Undefined index: a in myform.php on line 10

In the first one your parens were set so that you were checking $_POST
whether or not the particular index was set. In the second one the
value in $_POST is always checked, whether it is valid or not.

The following code does what you want:

<?php
$part1[]="a";
$part1[]="b";
$part1[]="c";
for($i=0; $i<count($part1); $i++)
{
echo "<td><input name=\"".$part1[$i]."\" type=\"checkbox\"
value=\"true\"";

if( (isset($_POST[$part1[$i]]) && ($_POST[$part1[$i]] != "") ))
echo " checked";

echo ">". (isset($_POST[$part1[$i]]) ? "-" . $_POST[$part1[$i]] :
"" ) . "</td></tr>";
}
?>


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

[Back to original 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

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