• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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";
?>
0
jmsloan
Asked:
jmsloan
2 Solutions
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now