Hi Everyone,
I have some dynamic code for a checkbox that works on the frontside, but if I try and go back (IE, return to the page after submitting it) the test for the POST data should retain the checkboxes selected and it is not.
Here is my code.
$grades = array('K-3', '4', '5', '6', '7', '8', '9', '10', '11', '12', 'Other');
$x='5'; //tabindex value
$zz='0';
foreach($grades as $grade) {
$selected = ((isset($_POST['grade']) && !empty($_POST['grade'])) ? in_array($grade, $_POST['grade']) : false);
echo '<input type="checkbox" id="chk" name="GRADE['.$zz.']" tabindex="'.$x.'" value="GRADE ' . $grade . '"' . ($selected ? 'checked' : '') . '>';
if($grade=='Other') {
echo ' <label for="' . $grade . '_chckbx">' . $grade .' ';
} else {
echo ' <label for="' . $grade . '_chckbx">' . $grade . ' |';
}
$x++;
$zz++;
}
The HTML result of the above is (to save you from putting in your editor).
<input type="checkbox" id="chk" name="GRADE[0]" tabindex="5" value="GRADE K-3">
<label for="K-3_chckbx">K-3 |<input type="checkbox" id="chk" name="GRADE[1]" tabindex="6" value="GRADE 4">
<label for="4_chckbx">4 |<input type="checkbox" id="chk" name="GRADE[2]" tabindex="7" value="GRADE 5">
<label for="5_chckbx">5 |<input type="checkbox" id="chk" name="GRADE[3]" tabindex="8" value="GRADE 6">
<label for="6_chckbx">6 |<input type="checkbox" id="chk" name="GRADE[4]" tabindex="9" value="GRADE 7">
<label for="7_chckbx">7 |<input type="checkbox" id="chk" name="GRADE[5]" tabindex="10" value="GRADE 8">
<label for="8_chckbx">8 |<input type="checkbox" id="chk" name="GRADE[6]" tabindex="11" value="GRADE 9">
<label for="9_chckbx">9 |<input type="checkbox" id="chk" name="GRADE[7]" tabindex="12" value="GRADE 10">
<label for="10_chckbx">10 |<input type="checkbox" id="chk" name="GRADE[8]" tabindex="13" value="GRADE 11">
<label for="11_chckbx">11 |<input type="checkbox" id="chk" name="GRADE[9]" tabindex="14" value="GRADE 12">
<label for="12_chckbx">12 |<input type="checkbox" id="chk" name="GRADE[10]" tabindex="15" value="GRADE Other">
<label for="Other_chckbx">Other
And from the above code, if I choose K-3 and 4, the print_r of the grade array gives me...
[grade] => Array
(
[0] => GRADE K-3
[1] => GRADE 4
)
So I am not sure why my test fails.
I appreciate the help in advance.
Regard,
Don