Using array with checkboxes and POST method

I can not get my checkbox arrays to display properly.  I wrote a small script so you can tell what happens.  Basically it load 3 checkboxes in an array called mycount. I also have a submit button that reload the current page with a couple of GET variables.

If you select the 3rd check box only and click submit the data displayed on the screen is

Test1 - On
Test2 -
Test3 -

It should say
Test1 -
Test2 -
Test3 - On

Can anyone tell my why php and the POST method are not recognizing the right index?


<?php
if($_GET[first] != "N"){
   $count = 3;
   $mycheckbox = "<form action='./test.php?first=N&count=$count' method='POST'>";
   for($i=0;$i<$count;$i++){
      $mycheckbox .= "<input type='checkbox' name='mycount[]'>Test$i<br>";
   }
   $mycheckbox .= "<input type='submit' name='submit' value='submit'>
                            </form>";
}else{  
   for($i=0;$i<$_GET[count];$i++){
      $mycheckbox .= "Test$i - $mycount[$i]<br>";
   }
}
echo "$mycheckbox";
?>
LVL 3
jmsloanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PetrTomenendalCommented:
browser sends only checked checkboxes and you have no specific index assigned to them - so server receives only one element for mycount array and it gets id 0.
change this:
$mycheckbox .= "<input type='checkbox' name='mycount[]'>Test$i<br>";
to this:
$mycheckbox .= "<input type='checkbox' name='mycount[$i]'>Test$i<br>";

and it should work
0
suresh_aspCommented:
try this,

<?php
if($_GET[first] != "N"){
   $count = 3;
   $mycheckbox = "<form action='./checkboxes.php?first=N&count=$count' method='POST'>";
   for($i=0;$i<$count;$i++){
      $mycheckbox .= "<input type='checkbox' name='mycount[$i]'>Test$i<br>";
   }
   $mycheckbox .= "<input type='submit' name='submit' value='submit'>
                            </form>";
}else{
   for($i=0;$i<$_GET[count];$i++){
      $mycheckbox .= "Test$i - ".$mycount[$i]."<br>";
   }
}  
echo "$mycheckbox";
?>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.