Reputation: 55
I've attempted to store POST values into selected checkboxes, I'm having some problem with it storing data after the form is submitted. This is what I have:
<fieldset>
<legend>Categories:</legend>
<?php $sql3 = 'SELECT id, name FROM category';
foreach ($dbConnection->query($sql3) as $category) { ?>
<div>
<label for="category<?php echo($category['id']);?>">
<input type="checkbox" name="categories[]" id="category<?php echo($category['id']); ?>" value="<?php echo($category['id']); ?>"
<?php if(isset($_POST['category']) && $_POST['category'] == $category['id']) { echo 'checked'; } ?>>
<?php echo($category['name']); ?></label>
</div>
<?php } ?>
</fieldset>
Any chance someone can lend a hand with it?
Upvotes: 0
Views: 140
Reputation: 16436
change your if statement to this:
<?php if(isset($_POST['categories']) && in_array($category['id'],$_POST['categories'])) { echo 'checked'; } ?>>
<?php echo($category['name']); ?></label>
Upvotes: 1