0

Im trying to populate the POST checkboxes this way

foreach ($chk as $key => $value) {
    if (isset($_POST[$key])) $chk[$key][$_POST[$key]] = 'checked="checked"';
}

But for some reason is not populating them for the following checkboxes

 <input type="checkbox" name="chk[]" value="A" />A
 <input type="checkbox" name="chk[]" value="B" />B
 <input type="checkbox" name="chk[]" value="C" />C

Any help will be appreciate it.

2 Answers 2

3

Checkboxes won't populate themselves by magic, you must actually insert the checked="checked" there. And I think you're no better off populating the data beforehand, this is usually the simplest way:

<input type="checkbox" name="chk[]" value="A" <?php if(isset($_POST['chk']['A'])) echo 'checked="checked"'; ?>/>A
<input type="checkbox" name="chk[]" value="B" <?php if(isset($_POST['chk']['B'])) echo 'checked="checked"'; ?>/>B
<input type="checkbox" name="chk[]" value="C" <?php if(isset($_POST['chk']['C'])) echo 'checked="checked"'; ?>/>C
Sign up to request clarification or add additional context in comments.

Comments

1

The browser won't care whether you pre-populate some PHP variables in your script: It sees only the generated HTML. You need to write the "checked='checked'" directly into your HTML output.

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.