Jump to content

Hassle With Foreach Or How Check Box Data Is Sent From A Form


wongadob

Recommended Posts

I am having a problem which has had me going round in circles for ages. I am pretty sure I am using for each correctly but I am getting bizarre results. Here is my code first

  $x=0;  print "<p>The following surveys were selected:-";  foreach ($_POST["SelectedSurveys"] as $value)  {   $SelectedSurveys[$x]=$value;   if ($SelectedSurveys[$x])   {	print "{$SelectedSurveys[$x]},";   }   else   {	print"off,";   }   $x+=1;  }  Print "</p><br/>";

I have a form on the calling page that has a list of surveys with a check box for each. When I run through thus it just prints on,on,on,on,on regardless of if they are checked or not. However the number of on's does represent the total number of checked boxes. my question is - why is it not printing 'off' for those that are off? what exactly is a checkbox array actually sending. I have also tried to check for null, but don;t get anything there either. BTW $SelectedSurveys, although having the same name as the array in the post is intentional. I also assume this is not a problem.

Link to comment
Share on other sites

Checked the print_r and it seems you are right it does not store any relation to the actualy survey that is clicked. it only seems to contain X number of elements where X is the amount of boxes ticked. So how do you relate that back to the items of the checkbox array? I populate straight from a database as shown below.

	 while($row = mysql_fetch_row($result))	  {	   print "<input type ='checkbox' class = 'selectedsurvey' name = 'SelectedSurveys[]' checked='checked'/>";	   print "<label>$row[1]</label>";	   print "<br/>\n";	 	  }

Do I need to store extra information so I have a relation. Thanks

Link to comment
Share on other sites

Not sure where relations come into this, but you can just give each checkbox a different name in the form.

Link to comment
Share on other sites

you will have pass the number checkbox inputs you send, then the output should be similar to below

<form action="phpprob07.php" method="post" name="myform"><input name="SelectedSurveys[0]" type="checkbox" value="survey1" /><input name="SelectedSurveys[1]" type="checkbox" value="survey2" /><input name="SelectedSurveys[2]" type="checkbox" value="survey3" /><input name="SelectedSurveys[3]" type="checkbox" value="survey4" /><input name="SelectedSurveys[4]" type="checkbox" value="survey5" /><input name="SelectedSurveys[5]" type="checkbox" value="survey6" /><input type="hidden" value="6" name="cb_count" /><input name="submit" type="submit" value="submit" /></form>

now the checkboxes have index ref as in SelectedSurveys[0], you can use this as a key ref in you foreach loop, while you use a 'for loop' to loop through the number checkbox elements ($_POST["cb_count"]) you have and compare it with $key value.

<?phpif(isset($_POST["SelectedSurveys"])){print_r($_POST);  $x=0;  $checked=false;  print "<p>The following surveys were selected:-";         for($i=0;$i<($_POST["cb_count"]);$i++)          {  foreach ($_POST["SelectedSurveys"] as $key => $value)      {           if ((int)$key == $i)               {               $SelectedSurveys[$x]=$value;	        print "{$SelectedSurveys[$x]},";            $checked=true;            }          }        if($checked==false)        {        print "off,";        $x++;        }        else        {        $checked=false;        $x++;        }             }  print "</p><br/>";  }?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...