1

I have a comment section handling name/phone/comment/etc. I have a checkbox in this form as well. I get the error exception to produce when the checkbox set is empty. But if boxes are checked, and other areas aren't satisfied, I can't get the boxes to stay checked while the errors are thrown elsewhere and the page is reloaded.

I tried setting the values and $response in the if statements to yes, but if one box is checked, when the page reloads all boxes are checked.

PHP:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["response"])) {
        $responseErr = "Response is required";
    } else {
        $response = test_input($_POST["response"]);
    }
}


How would you prefer us to respond? Choose all that apply.
<span class="error">* 
    <?php echo $responseErr;?>
</span> 
<br>
<input type="checkbox" name="response" <?php if (isset($response) && $response=="Call") echo "checked";?> value="Call">Call
<input type="checkbox" name="response" <?php if (isset($response) && $response=="Text") echo "checked";?> value="Text">Text
<input type="checkbox" name="response" <?php if (isset($response) && $response=="Email") echo "checked";?> value="Email">E-mail
<br><br>
1
  • What will happen if multiple checkboxes are checked? You will get only 1 value in $response. And only 1 checkbox will be checked. Commented Aug 19, 2015 at 15:44

2 Answers 2

1

An alternate solution could be to give each checkbox a unique name (tel-response, email-response, text-response) and then check each individually. Sure, it means slightly more code, but I it is easy to implement and manage later on.

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

1 Comment

you're right. had to give each item their own unique name and check each individually. Also had to edit the error checking code for each unique check to all entries each time. I'll post the answer
0

Solved:

Someone mentioned I should give each input their own name.

How would you prefer us to respond? Choose all that apply.<span class="error">* <?php echo $responseErr;?></span><br>
                <input type="checkbox" name="callResponse" <?php if (isset($callResponse) && $callResponse=="Yes") echo "checked";?> value="Yes">Call
                <input type="checkbox" name="textResponse" <?php if (isset($textResponse) && $textResponse=="Yes") echo "checked";?> value="Yes">Text
                <input type="checkbox" name="emailResponse" <?php if (isset($emailResponse) && $emailResponse=="Yes") echo "checked";?> value="Yes">E-mail<br><br>

Also, in error checking, you need to test all 3 inputs against each other for an empty entry. Otherwise even if a box is checked, the error message will still display.

if (empty($_POST["callResponse"]) && empty($_POST["textResponse"]) && empty($_POST["emailResponse"])) {
        $responseErr = "Response is required";
    } else {
        $callResponse = test_input($_POST["callResponse"]);
    }

    if (empty($_POST["callResponse"]) && empty($_POST["textResponse"]) && empty($_POST["emailResponse"])) {
        $responseErr = "Response is required";
    } else {
        $textResponse = test_input($_POST["textResponse"]);
    }

    if (empty($_POST["callResponse"]) && empty($_POST["textResponse"]) && empty($_POST["emailResponse"])) {
        $responseErr = "Response is required";
    } else {
        $emailResponse = test_input($_POST["emailResponse"]);
    }

Comments

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.