benphp Posted September 12, 2008 Share Posted September 12, 2008 I have a registration form that includes 5 checkboxes as an array: X Session1 X Session2 Session3 X Session4 X Session5 all set up as name="sid[]" array. If the user unchecks Session 3, I want to update the database, setting all the checked sessions to 'e' (enroll) and the one unchecked session to 'u' (unenroll). When I submit, I want to update the database for ALL the Sessions, setting the Status to 'e' (enroll) or 'u' (unenroll). This is what I tried: $count = 0; foreach ($sidAll as $temp) { if ($sid[$count] == NULL) { print "<br />UPDATE senroll SET status = 'e' WHERE sid = '$sid[$count]' <br>"; } else { print "<br />UPDATE senroll SET status = 'u' WHERE sid = '$sid[$count]' <br>"; } $count++; } It compares the user selected sid array with the database sid array. It doesn't work though, because the user selected sid contains an undefined element - the sid that was unchecked. Any help? Link to comment https://forums.phpfreaks.com/topic/123971-checkbox-processing-help-how-to-find-the-unchecked-box/ Share on other sites More sharing options...
kenrbnsn Posted September 12, 2008 Share Posted September 12, 2008 Can you post the form source? Ken Link to comment https://forums.phpfreaks.com/topic/123971-checkbox-processing-help-how-to-find-the-unchecked-box/#findComment-639982 Share on other sites More sharing options...
benphp Posted September 12, 2008 Author Share Posted September 12, 2008 Here's the HTML, which is really generated by PHP: <?php <input type="checkbox" value="1" name="sid[]" checked>Monday (2008-03-18)<br /> <input type="checkbox" value="2" name="sid[]" checked>Tuesday (2008-03-19)<br /> <input type="checkbox" value="3" name="sid[]" checked>Wednesday (2008-03-20)<br /> <input type="checkbox" value="4" name="sid[]" checked>Thursday (2008-03-22)<br /> <input type="checkbox" value="5" name="sid[]" checked>Friday (2008-03-23)<br /> <input type="hidden" value="5" name="sessionCount"> <input type="hidden" value="1" name="sidAll[]"> <input type="hidden" value="2" name="sidAll[]"> <input type="hidden" value="3" name="sidAll[]"> <input type="hidden" value="4" name="sidAll[]"> <input type="hidden" value="5" name="sidAll[]"> ?> The form submits to a sql.php page where I run this: <?php $sidAll = $_POST['sidAll']; $sid = $_POST['sid']; $sessionCount = $_POST['sessionCount']; //count the number of sessions selected $sessionCountSelected = count($sid); //if the number selected and number defined differ, then update the session enrollment table if ($sessionCount != $sessionCountSelected) { //cycle through ALL the sessions defined and determine which ones were DESELECTED $count = 0; foreach ($sidAll as $temp) { if (!in_array($sid[$count])) { print "<br />UPDATE senroll SET status = 'e' WHERE sid = '$sid[$count]' <br>"; } else { print "<br />UPDATE senroll SET status = 'u' WHERE sid = '$sid[$count]' <br>"; } $count++; } } ?> which of course isn't working. It can't read an undefined element in the array, so I get an error. Link to comment https://forums.phpfreaks.com/topic/123971-checkbox-processing-help-how-to-find-the-unchecked-box/#findComment-640006 Share on other sites More sharing options...
kenrbnsn Posted September 12, 2008 Share Posted September 12, 2008 You can do something like this to pre-initialize the sid[] array: <?php if (!empty($_POST)) echo '<pre>' . print_r($_POST,true) . '</pre>'; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> </head> <body> <form action="" method="post"> <?php for($i=1;$i<6;$i++) echo '<input type="hidden" value="U" name="sid[' . $i . ']">'; ?> <input type="checkbox" value="1" name="sid[1]" checked>Monday (2008-03-18)<br /> <input type="checkbox" value="2" name="sid[2]" checked>Tuesday (2008-03-19)<br /> <input type="checkbox" value="3" name="sid[3]" checked>Wednesday (2008-03-20)<br /> <input type="checkbox" value="4" name="sid[4]" checked>Thursday (2008-03-22)<br /> <input type="checkbox" value="5" name="sid[5]" checked>Friday (2008-03-23)<br /> <input type="hidden" value="5" name="sessionCount"> <input type="hidden" value="1" name="sidAll[1]"> <input type="hidden" value="2" name="sidAll[2]"> <input type="hidden" value="3" name="sidAll[3]"> <input type="hidden" value="4" name="sidAll[4]"> <input type="hidden" value="5" name="sidAll[5]"> <br> <input type="submit" name="submit" value="Test It"> </form> </body> </html> Be warned: I've never seen this method fail, but the order that values are presented to the processing script is not well defined, so a new browser might break this method. Ken Link to comment https://forums.phpfreaks.com/topic/123971-checkbox-processing-help-how-to-find-the-unchecked-box/#findComment-640018 Share on other sites More sharing options...
sasa Posted September 12, 2008 Share Posted September 12, 2008 try <?php $sidAll = $_POST['sidAll']; $sid = $_POST['sid']; //$sessionCount = $_POST['sessionCount']; //count the number of sessions selected //$sessionCountSelected = count($sid); //if the number selected and number defined differ, then update the session enrollment table //if ($sessionCount != $sessionCountSelected) { //cycle through ALL the sessions defined and determine which ones were DESELECTED //$count = 0; foreach ($sidAll as $temp) { if (in_array($temp,$sid)) { print "<br />UPDATE senroll SET status = 'e' WHERE sid = '$temp' <br>"; } else { print "<br />UPDATE senroll SET status = 'u' WHERE sid = '$temp' <br>"; } // $count++; // } } ?> Link to comment https://forums.phpfreaks.com/topic/123971-checkbox-processing-help-how-to-find-the-unchecked-box/#findComment-640022 Share on other sites More sharing options...
benphp Posted September 12, 2008 Author Share Posted September 12, 2008 sasa - that works great! Thank you! and it looks so easy... Link to comment https://forums.phpfreaks.com/topic/123971-checkbox-processing-help-how-to-find-the-unchecked-box/#findComment-640029 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.