I have a code like this:
$values = array('1A', '2B', '3C', '4D', '5E');
$checked = array('1A_check', '2B_check', '3C_check', '4D_check', '5E_check');
$description = array('Description1', 'Description2', 'Description3', 'Description4', 'Description5');
for ($i=0; $i<count($values); $i++) {
$$checked[$i] = ""; //Setting this to null since this variable will be set to checked or not in the later step
$checkbox_form[] = '<input type="checkbox" name="checkbox[]" value="'. $values[$i] .'"'. $$checked[$i] .'>
'. $description[$i] .' <br />';
}
foreach ($checkbox_form as $value) { //Rending the Form
echo $value;
}
This code renders the form like this:
<input type="checkbox" name="checkbox[]" value="1A">
Description1 <br />
<input type="checkbox" name="checkbox[]" value="2B">
Description2 <br />
<input type="checkbox" name="checkbox[]" value="3C">
Description3 <br />
<input type="checkbox" name="checkbox[]" value="4D">
Description4 <br />
<input type="checkbox" name="checkbox[]" value="5E">
Description5 <br />
So far so good. What I am trying to do next is, when the user select some of the checkboxes from the box and clicks 'preview', I want them to go to a page which previews the form with the selected checkboxes 'checked'. So I have a code like this to do that:
//After checking what values were posted in the previous screen
$checkbox_posted = array('1A_check', '2B_check'); //Storing the posted checkboxes to this array
if (count($checkbox_posted) != 0) {
foreach ($checkbox_posted as $item) {
$$item = ' checked';
}
}
I thought the above variable variable
code will add the 'checked' value to $1A_check
and $2B_check
variables in line #1 and Line #2 of the form, but it doesnt and the checkboxes arent checked. I thought the form should output like this:
<input type="checkbox" name="checkbox[]" value="1A" checked>
Description1 <br />
<input type="checkbox" name="checkbox[]" value="2B" checked>
Description2 <br />
<input type="checkbox" name="checkbox[]" value="3C">
Description3 <br />
<input type="checkbox" name="checkbox[]" value="4D">
Description4 <br />
<input type="checkbox" name="checkbox[]" value="5E">
Description5 <br />
But instead it outputs without passing the checked value. So it isint working. What have I done wrong?