giraffemedia Posted January 19, 2011 Share Posted January 19, 2011 Hi I have a form populated with an array of checkboxes (with labels). When I submit the form to the same page none of the checkbox values are remembered. What is the best way of achieving this? If I echo the checked box values it tells me which ones were selected but I cannot get the actual boxes to remember if they were checked or not. Here is what I have… $checked = $_POST['issue']; foreach($checked as $item) { echo "Item $item was checked<br />"; } while ($row = mysql_fetch_array($issue_query)) { $issues[] = '<span style="display:block;margin:0px 0px 10px 60px;"><input name="issue[]" type="checkbox" style="width:auto;" value="'.$row['issue_number'].'">Issue <strong>'.$row['issue_number'].'</strong> '.$row['issue_month'].' '.$row['issue_year'].'<span>'; } Any ideas? Thanks, James Link to comment https://forums.phpfreaks.com/topic/224979-checkbox-array-from-mysql-need-to-remember-if-checked-on-form-self/ Share on other sites More sharing options...
Porl123 Posted January 19, 2011 Share Posted January 19, 2011 $checked = $_POST['issue']; foreach($checked as $item) { echo "Item $item was checked<br />"; } while ($row = mysql_fetch_array($issue_query)) { $issues[] = '<span style="display:block;margin:0px 0px 10px 60px;"><input name="issue[]" type="checkbox" style="width:auto;" value="'.$row['issue_number'].'"'.(in_array($row['issue_number'],$_POST['issue']) ? ' checked="checked"' : '').'>Issue <strong>'.$row['issue_number'].'</strong> '.$row['issue_month'].' '.$row['issue_year'].'<span>'; } You could try that. I'm not 100% sure, though. D: Link to comment https://forums.phpfreaks.com/topic/224979-checkbox-array-from-mysql-need-to-remember-if-checked-on-form-self/#findComment-1162003 Share on other sites More sharing options...
giraffemedia Posted January 20, 2011 Author Share Posted January 20, 2011 Works like a charm Porl123! I had to add an if isset statement around the whole script to stop it giving me errors if the array was empty but apart from that great. Thanks, James Link to comment https://forums.phpfreaks.com/topic/224979-checkbox-array-from-mysql-need-to-remember-if-checked-on-form-self/#findComment-1162385 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.