Hello I will get right to it. I have a form like this <form> <div class="section2"> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> </div> <div class="section2"> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> </div> </form> HTML: Then i use php to loop through all the check-boxes of each section for the ones that are checked save it as check and if not checked save it to database as not checked foreach($_POST['checkboxes'] as key=>value) { $checked_box= (isset(value) ? "checked" : "notchecked"); //then here code to save results(above checked_box variable) for each checkbox in database } PHP: But the problem i am having with this is that, let say I went ahead and selected check-box number three of say section one for example, when ever it gets saved to the database, checkbox three ends up getting saved as unchecked, even though it was checked while checkbox one, the first checkbox get saved as selected, even though it was not selected. Whats really happening here, why does this happen. Any help on this would be greatly appreciated Thanks
You would have to provide the rest of the code as well, for instance how you save stuff to the database. I'm guessing there is a wrongly assigned loop or value to be used in a loop, or something like that.
This happens because only the checked checkboxes get sent while the unchecked checkboxes or not sent. What you can do instead is to add a value and check what the value of the checked checkbox is. <form> <div class="section2"> <input type="checkbox" value="1" name="checkboxes[]"/> <input type="checkbox" value="2" name="checkboxes[]"/> <input type="checkbox" value="3" name="checkboxes[]"/> <input type="checkbox" value="4" name="checkboxes[]"/> </div> <div class="section2"> <input type="checkbox" value="5" name="checkboxes[]"/> <input type="checkbox" value="6" name="checkboxes[]"/> <input type="checkbox" value="7" name="checkboxes[]"/> <input type="checkbox" value="8" name="checkboxes[]"/> </div> </form> Code (markup): And then check it like this. foreach($_POST['checkboxes'] as $key=>$value) { if ($value == 3) { //The third checkbox was checked! } //then here code to save results(above checked_box variable) for each checkbox in database } PHP:
Try this. <form> <div class="section2"> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> </div> <div class="section2"> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> <input type="checkbox" name="checkboxes[]"/> </div> </form> HTML: foreach($_POST['checkboxes'] as $key=>$value) { if(isset($_POST['checkboxes'][$key])) { // checkbox is checked } else { // checkbox is not checked } } PHP:
without a unique value identifying them it is just a bulk array and would have to be implode/explode better to give them separate IDs/value/name
Why are you using DIV for obvious fieldsets, why are you assigning name attributes in a manner that you would have NO clue which one is which or from which section, (much less the uselessly vague classnames there...), lack of action or method... Your markup is incomplete gibberish, not certain what you would even expect to do with that mess server side much less how to go about it.