Try this at home:
Write a form to display a list of checkboxes in PHP:
$checkboxes = array('one' => 1, 'two' => 2, 'three' => 3);
$html = "<html><body><form name=\"myForm\" action=\"this\" method=\"post\"><ul>Check Test";
foreach ($checkboxes as $id => $value)
{
$checkVal = (isset($_POST['check_test'][$id])) ? 'true' : 'false';
$checkList = "<li><input type=\"checkbox\" name=\"check_test[]\" id=\"$id\"
value =\"$value\" checked=\"$checkVal\" />$name = $value</li>\n";
}
$checkList .= "<input type=\"submit\" value=\"Submit\">";
echo $html.$checkList."</ul></body></html>";
You will find that no matter what you put into the $checkVal assignment, when you hit submit, your checkboxes will all be checked. My workaround is to expand the $checkVal variable to set the entire “checked=” status to either something or nothing. I’m just surprised that Firefox (rather, Iceweasel) is only looking to see if the attribute is set at all. Why even have it if whatever it’s set to is irrelevant?
Look at the source html. The selected attribute will display whatever you put into the checkval variable. The DOM specification states:
checkboxObject.checked=true|false
Here is my output:
<ul><li><input type="checkbox" name="check_test[]" id="one"
value ="1" checked="false" />one= 1</li>
<li><input type="checkbox" name="check_test[]" id="two"
value ="2" checked="false" />two= 2</li>
<li><input type="checkbox" name="check_test[]" id="three"
value ="3" checked="false" />three= 3</li>
And lo and behold! Every box is checked. Contrary to what I’ve explicitly expressed in the html.
The false is ignored. If the check attribute exists the box is checked.