FeralReason Posted May 4, 2009 Share Posted May 4, 2009 (HTML Form, using PHP) I have a form that, in the event of a user error, I want displayed again with the values just entered. I have solved this for the text boxes but, no matter what I try I have not been able retain the checked status of the checkboxes. Does anyone know how to do this ? Quote Link to comment https://forums.phpfreaks.com/topic/156735-retaining-checked-status-of-checkboxes-in-a-form/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 This should belong in the PHP forum, but here's my idea. You create an associative array of values for those checkboxes. Say you have this: <input type="checkbox" name="foods[]" value="rice" /> Rice <input type="checkbox" name="foods[]" value="ramen" /> Ramen <input type="checkbox" name="foods[]" value="chicken" /> Chicken <input type="checkbox" name="foods[]" value="pizza" /> Pizza Then have this array (assuming you submitted the form in post method) $food_values = array(); $foods = $_POST['foods']; foreach ($foods as $value) { $food_values[] = $value; } That would set $food_values to have all the values of the checkboxes that are checked. I guess you can have another array that contains a list of all checkboxes. If you have them in the DB, query the DB, but you get the point right? Quote Link to comment https://forums.phpfreaks.com/topic/156735-retaining-checked-status-of-checkboxes-in-a-form/#findComment-825352 Share on other sites More sharing options...
Axeia Posted May 4, 2009 Share Posted May 4, 2009 To set a checkbox as checked use the attribute checked="checked" on the input. <input type="checkbox" checked="checked" /> Quote Link to comment https://forums.phpfreaks.com/topic/156735-retaining-checked-status-of-checkboxes-in-a-form/#findComment-825585 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.