tags:

views:

307

answers:

2

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.

+1  A: 

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.

Pekka
+1  A: 

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
Tatu Ulmanen