Domonkos Zoltán Domonkos Zoltán - 2 months ago 12
PHP Question

(Checkbox array) reload form and values PHP

So i have a form and i don't want to clear fields when form submitted.

I can do this for simple input types :

<input name="name" value="<?php echo $_POST['name']; ?>>


But for array checkboxes

<input type="checkbox" name="ans[]" value="2" class="grp1" onclick="phaseCheck();">
<input type="checkbox" name="ans[]" value="3" class="grp1" onclick="phaseCheck();">


How I can reload and check which checkboxes checked by the user?

Thanks!

Answer Source

Use in_array function

<input type="checkbox" <?php if(in_array(2,$_POST['ans'])) { echo "checked"; } ?> name="ans[]" value="2" class="grp1" onclick="phaseCheck();">
<input type="checkbox" <?php if(in_array(3,$_POST['ans'])) { echo "checked"; } ?>  name="ans[]" value="3" class="grp1" onclick="phaseCheck();">