PDA

View Full Version : Multiple checkboxes


houssam_ballout
02-08-2008, 02:28 PM
Hello all,
I'd multiple checkboxs and radio buttons , and they are generated automatically based on the database that I'd (mysql)..

on my page, I had queried all the questions and number of options, part of the code that I think you need is:

for($i=1;$i<=$NbOptions;$i++)
{
if($MultiAnswer == 'yes')
{
echo "<input type='checkbox' name='box[]' value='$idQuestion_$i' />" . $row2['option'.$i];
echo "<br />";
}

else
{
echo "<input type='radio' name='box[]' value='' />" . $row2['option'.$i];
echo "<br />";
}


}

when I press save ,
I use
if(isset($_POST['save']))

//here I want to loop through all the box[]?
and see if its checked or no

hammer65
02-08-2008, 02:57 PM
Well first of all, you are going to have a hard time with treating the checkboxes and radiobuttons the same using array syntax for the names.

As you know, radio buttons provide exclusive choices whereas checkboxes are non-exclusive. Naming the radio buttons with array syntax won't bring back the same results as they would with the checkboxes. The radios will only bring back single value, and only if one of them is checked.

The checkboxes will only return a value if they are checked. On submission, you will have to check that your "box" variable even exists, and that it is an array before attempting to loop through it.

houssam_ballout
02-08-2008, 03:20 PM
ok lets suppose that I dont need the radio button
I need help regarding checkboxes?

Andrew Johnson
02-08-2008, 03:46 PM
Instead of using this format:


<input type="checkbox" name="box[]" value="$idQuestion_$i" />


Why not use this:


<input type="checkbox" name="$idQuestion_$i" />

hammer65
02-08-2008, 03:53 PM
Instead of using this format:


<input type="checkbox" name="box[]" value="$idQuestion_$i" />
Why not use this:


<input type="checkbox" name="$idQuestion_$i" />


You would still have to use array syntax for the names of the checkboxes in order to get all values back.


echo '<input type="checkbox" name="'.$idQuestion.'['.$i.']" />';