Note that there are some explanatory texts on larger screens.

plurals
  1. POCheckbox "checked"-value jumps up to earlier array values, when array is empty
    text
    copied!<p>I have an array of checkboxes name="box[]". Through PHP I make sure that they're checked after they're submitted by echoing "checked='checked'" if they were checked at submit event.</p> <p>Now, if I check the third box, the value jumps down to the first checkbox after submit, since the array was empty up until the third checkbox. Same, if I check the 2nd and 3rd checkbox, they jump down to 1st and 2nd after submit. This is the code I'm using:</p> <pre><code>&lt;form method="post"&gt; &lt;input type="checkbox" name="box[]" value="true" &lt;?php if ($box[0] == true) echo "checked='checked'"; ?&gt;&gt;&lt;br&gt; &lt;input type="checkbox" name="box[]" value="true" &lt;?php if ($box[1] == true) echo "checked='checked'"; ?&gt;&gt;&lt;br&gt; &lt;input type="checkbox" name="box[]" value="true" &lt;?php if ($box[2] == true) echo "checked='checked'"; ?&gt;&gt;&lt;br&gt; &lt;p&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; </code></pre> <p>Try it at: <a href="http://experiencerapanui.com/selecttest.php" rel="nofollow">http://experiencerapanui.com/selecttest.php</a></p> <p>Can I make the checkboxes fill up the array with a value "false" or whatever, if the box is unchecked? Which way should I go?</p> <p><em><strong></em>**<em>*</em>***<em></strong> EDIT <strong></em>**<em>*</em>**<em>*</em></strong></p> <p>Thanks to phant0m, I managed to come up with a solution:</p> <pre><code>&lt;form method="post"&gt; &lt;input type="checkbox" name="box[]" value="1" &lt;?php if (in_array("1", $box)) echo "checked='checked'"; ?&gt;&gt;&lt;br&gt; &lt;input type="checkbox" name="box[]" value="2" &lt;?php if (in_array("2", $box)) echo "checked='checked'"; ?&gt;&gt;&lt;br&gt; &lt;input type="checkbox" name="box[]" value="3" &lt;?php if (in_array("3", $box)) echo "checked='checked'"; ?&gt;&gt;&lt;br&gt; &lt;p&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; </code></pre> <p>Putting unique values for the checkboxes, then if I find the value in the array $box[], the box is marked as checked.</p>
 

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