I know how to keep the checkboxes checked on form submit when there is an error, but I have a different issue now. I am using check boxes of the same name where people can click 1 or multiple. I want it to only keep the check boxes that the user checked filled if there is a problem like if they check q1 A q1B but not q1c I want only q1 A and Q1b to show checked when reloaded on an error. Right now on an error all checkboxes of the same name are checked. I have tried changing the name to q1[] but that did not work. Can you please take a look and let me know how to fix this?
Here is my code.
<tr>
<td style="width: 124px" class="style15">Tape Recorder<?php if(isset($problems['tape[]'])) {?><font color="red">*</font><?php } ?></td>
<td class="style9">
<input name="tape[]" id="tape1" type="checkbox" value="used before," <?php if(isset($_POST['tape[]'])) echo "checked"; ?>
</td>
<td class="style9">
<input name="tape[]" id="tape2" type="checkbox" value="helpful in past," <?php if(isset($_POST['tape[]'])) echo "checked"; ?> </td>
<td class="style9">
<input name="tape[]" id="tape3" type="checkbox" value="requesting from DACC" <?php if(isset($_POST['tape[]'])) echo "checked"; ?> </td>
<td class="style9">
<input name="tape[]" id="tape4" type="checkbox" value="NA" <?php if(isset($_POST['tape[]'])) echo "checked"; ?></td>
</tr>
<tr>
<td style="width: 124px">Note Taker <?php if(isset($problems['note'])) {?> <font color="red">*</font><?php } ?></td>
<td class="style9">
<input name="note" type="checkbox" value="used before," <?php if(isset($_POST['note'])) echo "checked"; ?>
</td>
<td class="style9">
<input name="note" type="checkbox" value= "been helpful in the past," <?php if(isset($_POST['note'])) echo "checked"; ?>
<td class="style9">
<input name="note" type="checkbox" value= "requesting from DACC" <?php if(isset($_POST['note'])) echo "checked"; ?>
<td class="style9">
<input name="note" type="checkbox" value="NA" <?php if(isset($_POST['note'])) echo "checked"; ?>
</tr>
A quick and dirty solution would be:
<input name="tape[]" id="tape[]" type="checkbox" value="NA" <?php if(isset($_POST['tape']) && is_array($_POST['tape']) && in_array('NA', $_POST['tape'])) echo 'checked="checked"'; ?> />
For that you need to change the 'NA' part for each different answer obviously. Though I would look at something like having a loop for repeated checkboxes or a callback function to determine whether or not to echo checked=checked
.
Thank you Mr. Code Here's my final code that I was able to figure out thanks to you
$mailBody .= "They requested additional information on ...\n\n";
$mailBody .= $moreinfo = join("\n", $_REQUEST["moreinfo"]);
$mailBody .= "\n\n";
$mailBody .= "They also had this to say...\n\n";
$mailBody .= "$comments\n\n";
//code on page
<input type="checkbox" name="moreinfo[selection1]" value="selection1" <?php if(isset($_POST['moreinfo']) && is_array($_POST['moreinfo']) && in_array('selection1', $_POST['moreinfo'])) echo 'checked="checked"'; ?> /> Selection 1<br>
<input type="checkbox" name="moreinfo[selection2]" value="selection2" <?php if(isset($_POST['moreinfo']) && is_array($_POST['moreinfo']) && in_array('selection2', $_POST['moreinfo'])) echo 'checked="checked"'; ?> /> Selection 2<br>
<input type="checkbox" name="moreinfo[selection3]" value="selection3" <?php if(isset($_POST['moreinfo']) && is_array($_POST['moreinfo']) && in_array('selection3', $_POST['moreinfo'])) echo 'checked="checked"'; ?> /> Selection 3<br>
I do not care that this looks crazy because it works.
You dont need to have the same name for checkboxes, only for radio buttons.
Radio buttons belong to a group so they have to have the same name but different values. Checkboxes however, can have different names for each checkbox! Therefore just change each checkbox to a differnent name
来源:https://stackoverflow.com/questions/9519207/keeping-checkboxes-checked-in-php-form