This is my code
<?php foreach ($categories as $cat) { ?>
<li>
<input id="category" name="category[]" type="checkbox" value="<?= $cat->term_id; ?>"
<?php if (isset($_GET['category'])) echo "checked='checked'"; ?>><?= $cat->name ?></input>
</li>
<?php } ?>
But when i submit the form the check boxes are all checked and what i wan't is to keep checked only the checkbox i checked not the others Example Below
The problem is because of this line,
Upon form submission
$_GET['category']
will be set, hence this conditionisset($_GET['category'])
will hold true for all the checkboxes. And that's why all the checkboxes are checked irrespective of which one you checked earlier. So yourforeach
loop should be like this: