Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck Box selected attribute set regardless of value
    primarykey
    data
    text
    <p>Try this at home:<br> Write a form to display a list of checkboxes in PHP:</p> <pre><code>$checkboxes = array('one' =&gt; 1, 'two' =&gt; 2, 'three' =&gt; 3); $html = "&lt;html&gt;&lt;body&gt;&lt;form name=\"myForm\" action=\"this\" method=\"post\"&gt;&lt;ul&gt;Check Test"; foreach ($checkboxes as $id =&gt; $value) { $checkVal = (isset($_POST['check_test'][$id])) ? 'true' : 'false'; $checkList = "&lt;li&gt;&lt;input type=\"checkbox\" name=\"check_test[]\" id=\"$id\" value =\"$value\" checked=\"$checkVal\" /&gt;$name = $value&lt;/li&gt;\n"; } $checkList .= "&lt;input type=\"submit\" value=\"Submit\"&gt;"; echo $html.$checkList."&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;"; </code></pre> <p>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? </p> <p>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 </p> <p>Here is my output:</p> <pre><code>&lt;ul&gt;&lt;li&gt;&lt;input type="checkbox" name="check_test[]" id="one" value ="1" checked="false" /&gt;one= 1&lt;/li&gt; &lt;li&gt;&lt;input type="checkbox" name="check_test[]" id="two" value ="2" checked="false" /&gt;two= 2&lt;/li&gt; &lt;li&gt;&lt;input type="checkbox" name="check_test[]" id="three" value ="3" checked="false" /&gt;three= 3&lt;/li&gt; </code></pre> <p></p> <p>And lo and behold! Every box is checked. Contrary to what I've explicitly expressed in the html. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload