记住表单复选框值

I want a form to remember the user's input whenever an error has occurred. I got this to work using for the text input using:

echo "<input id=\"quote_square\" type=\"text\" name=\"square\" placeholder=\"Square\" value=\"".  $_POST['square'] . "\">";

For checkboxes I can't get this to work. I did find this example of how this can be done:

   <input type="checkbox" name="subscribe" <?php echo (isset($_POST['opdracht'])?'checked="checked"':'') ?> />

But I'm echoing my entire form into php and saving the checkbox input into an array:

echo "<input id=\"quote_round\" type=\"text\" name=\"round\" placeholder=\"Round\" value=\"".  $_POST['round'] . "\">";
echo "<input id=\"quote_square\" type=\"text\" name=\"square\" placeholder=\"Square\" value=\"".  $_POST['square'] . "\">";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdracht[]\" value=\"Blue\">Blue</label>";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdracht[]\" value=\"Black\">Black</label>";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdracht[]\" value=\"Red\">Red</label>";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdracht[]\" value=\"White\">White</label>";

Can anybody give a suggestion how I can get the checkboxes to save the user's input?

Try

echo "<label class=\"checkbox\"><input type=\"checkbox\" ", 
    in_array("Blue", $_POST["opdracht"])?"checked='checked' ":"", 
    "name=\"opdracht[]\" value=\"Blue\">Blue</label>";
   echo '<input type="checkbox" name="subscribe"' .($yourData['opdracht']?'checked="checked"':'').' />';

Try this:

echo isset($yourData['opdracht']) ? 'checked="checked"' : '';

Like

 <input type="checkbox" name="subscribe" <?php  echo isset($yourData['opdracht']) ? 'checked="checked"' : ''; ?> />

I don't believe you're using the array correctly. This kind of array referencing is used where you have multiple versions of the same data being submitted. The array keys get filled in when the form is submitted, and values that are not checked will not be indexed, so you'll end up with different sized arrays, depending on how many boxes have been checked. Telling your values apart will then be problematic. It needs to be something like this;

echo "<input id=\"quote_round\" type=\"text\" name=\"round\" placeholder=\"Round\"     value=\"".  $_POST['round'] . "\">";
echo "<input id=\"quote_square\" type=\"text\" name=\"square\" placeholder=\"Square\" value=\"".  $_POST['square'] . "\">";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdrachtBlue\" value=\"Blue\">Blue</label>";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdrachtBlack\" value=\"Black\">Black</label>";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdrachtRed\" value=\"Red\">Red</label>";
echo "<label class=\"checkbox\"><input type=\"checkbox\" name=\"opdrachtWhite\" value=\"White\">White</label>";

You can then use the

<input type="checkbox" name="subscribe" <?php echo (isset($_POST['opdrachtRed'])?'checked="checked"':'') ?> />

to get if the checkbox has been previously selected.

Try this

<label class="checkbox"><input type="checkbox" checked= "<?php in_array('Blue', $deep['opdracht'])?'checked' :''; ?>" value="Blue">Blue</label>