86Stang Posted December 7, 2009 Share Posted December 7, 2009 I'm displaying a list of dates that the user selects in an earlier form via: <form action="?opt=enter_events" method="post"> <?php for ($i=strtotime($start_date);$i<=strtotime($stop_date);$i+=86400) { echo "<input type=\"checkbox\" name=\"event_date[]\" value=\"Y\"> " . $event_date; echo date('Y-m-d', $i) . "<br />"; } ?> </form> What I'd like to do is grab any checkbox that is checked and dump them into a table with each being its own record. Can someone put me on the right track? Link to comment https://forums.phpfreaks.com/topic/184315-inserting-selected-checkboxes-into-db/ Share on other sites More sharing options...
mrMarcus Posted December 7, 2009 Share Posted December 7, 2009 thing is, it's almost quicker to just write the code than explain it. "event_date[]" is going to be an array once submitted, so you're going to want to handle it as so: <?php if (isset ($_POST['submit'])) //your submit button; { if (is_array ($_POST['event_date'])) { foreach ($_POST['event_date'] as $v) { $sql = mysql_query (" INSERT INTO `your_table` (`event_date`) VALUES ('".mysql_real_escape_string($v)."') ") or trigger_error (mysql_error()); } } else { echo 'event_date is not an array.'; } } else { //form not submitted; } ?> EDIT: of course alter the query as per your db. Link to comment https://forums.phpfreaks.com/topic/184315-inserting-selected-checkboxes-into-db/#findComment-973081 Share on other sites More sharing options...
86Stang Posted December 7, 2009 Author Share Posted December 7, 2009 Awesome sauce! Link to comment https://forums.phpfreaks.com/topic/184315-inserting-selected-checkboxes-into-db/#findComment-973099 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.