2

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>
1

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

MrCode, that didn't work it gave me PHP Parse error: syntax error, unexpected T_ECHO in C:\Inetpub\wwwroot\DACC\ssc\accommodations3.php on line 767 which is this line code<input name="tape[]" id="tape[]" type="checkbox" value="Used before" <?php if(isset($_POST['tape']) && is_array($_POST['tape']) && in_array('Used before', $_POST['tape']) echo 'checked="checked"'; ?> /><span class="style15"></span> code
sorry couldn't figure out what I was doing wrong with the mini-markdown, haven't used that much
0

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.

Comments

-1

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

4 Comments

Radio buttons must have the same name to be a member of a group, but there is no restriction on other controls sharing a name.
Re edit: Changing the name is not needed and throws away the advantage of being able to easily process a group of checkboxes in a loop.
Thanks, Quentin. Researching the topic now!
Given the type of form yehuda, I kind of need the check boxes to be the same name, it would be a headache to process through the email part otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.