dimanche 19 avril 2015

maintain checkbox on dynamically created checkboxes

I've searched high and low and can't find an answer to what I'm looking for. I have a list of checkboxes that are created dynamically from a list of categories in a database. Now, what I want to do is to be able to maintain which checkboxes are checked when the page posts back to itself, but nothing i am doing is working =/



<?php
$mysqli = new mysqli(SERVER_NAME, USERNAME, PASSWORD, DATABASE_NAME);
if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); }
$sql = " SELECT TableID AS WebsiteCategoryID, Category FROM listwebsitecategories ORDER BY Category ASC ";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$stmt->bind_result($selectWebsiteCategoryID, $selectCategory);
while ($stmt->fetch() == true) :
?>
<div><input type="checkbox" name="websitecategories[]" id="websitecategories<?php echo($selectWebsiteCategoryID); ?>"<?php if (isset($_POST['websitecategories' . $selectWebsiteCategoryID]) == true) echo(' checked="checked"'); ?> value="<?php echo($selectWebsiteCategoryID); ?>" />&nbsp;&nbsp;<?php echo($selectCategory); ?></div>
<?php
endwhile;
$stmt->close();
?>


I assumed this would work, but it doesn't. I can still retrieve which checkboxes are check by getting an array ($websiteCategories = $_POST['websitecategories']) but maintaining their checked value on postback just won't work.. Please help.


Aucun commentaire:

Enregistrer un commentaire