tags:

views:

81

answers:

2

I had multiple checkbox which is generated using loop. After submitting the form I want to get the names of the selected individual checkbox to store it in database as id. Please help me.. Thanks in advance

Code i used for generating checkbox in loop:

while($arrayRow = mysql_fetch_assoc($rsrcResult)) 
  {
      $strA = $arrayRow["area_id"];
  $strB = $arrayRow["area_name"];
      echo "<div class=\"area_check\"><input type=\"checkbox\" id=\"covarea[] \" />$strB</input></div>";
  }

This code I used for getting names for it didnt worked. It only returned state of check box as ON

 while (list ($key,$val) = @each ($box)) 
{
  $aid=$val;
  echo $aid;
}
+2  A: 

If the set of checkboxes is marked up as so

<input type="checkbox" name="food[]" value="Cheese">
<input type="checkbox" name="food[]" value="Ham">

Then any checked values are accessed as an array from $_POST['food']

Obviously with a code example, as @rajasekar points out, it would be easier to recommend an approach

simonrjones
I meant to say as @sico87 pointed out, of course!You might also want to look at previous questions - http://stackoverflow.com/search?q=checkboxes+php
simonrjones
+1  A: 
  • The id for each element is optional, but should/must be unique.
  • Form elements must have a name attribute, or they will not be submitted (use "covarea[]" for the name for all the chexboxes.
  • Checkboxes must have a value attribute, or they will be submitted with value "on".
  • You must ensure that $strA and $strB do not have HTML meta characters or your generated HTML will be invalid.
  • Your example had a space after the "[]" in the id, btw.

Perhaps like this.

$n = 0;
while($arrayRow = mysql_fetch_assoc($rsrcResult)) 
{
     $strA = $arrayRow["area_id"];
     $strB = $arrayRow["area_name"];
     $n++;
     echo "<div class=\"area_check\"><input type=\"checkbox\" id=\"covarea${n}\" name=\"covarea[]\" value=\"${strA}\"/>$strB</input></div>";
}
NVRAM