son.of.the.morning Posted December 5, 2011 Share Posted December 5, 2011 I have a while loop of check box's with the SQL record id's set as there values. I want to be able to pass all the checked records to another page in single var's with the correct ID value in order to list the selected items on the on the next page. any idea's guys? Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted December 5, 2011 Share Posted December 5, 2011 Give all the checkboxes an array name such as: <input type="checkbox" name="checkBoxArray[]" ... Then give each individual checkbox a unique value corresponding to the row ID: <input type="checkbox" name="checkBoxArray[]" value="<?php echo $rowIDarray[1]; ?>" /> <input type="checkbox" name="checkBoxArray[]" value="<?php echo $rowIDarray[2]; ?>" /> Where $rowIDarray would be an array of all the unique row ID's the user can select. Then, to process the response, you simply need to loop through checkBoxArray, each of it's corresponding values with be the ID of a row which was checked. If it's empty, no items were checked. Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted December 5, 2011 Share Posted December 5, 2011 Actually, I may have made an error. I particularly hate trying to use the value attribute with checkboxes specifically because it can lead to this error. I typically try to avoid this type of logic because I can never remember if an unchecked checkbox whose name is a PHP array with an incrementing index will make a new array entry and set it to the boolean false. In fact, I think this is what it will do, and a heads up to your logic when processing the response: you'll have to make sure the ID you're getting back isn't equal to (==) the empty string ( '' ) because unchecked checkboxes will probably make a new array index that is set to the empty string. Quote Link to comment Share on other sites More sharing options...
kicken Posted December 5, 2011 Share Posted December 5, 2011 I can never remember if an unchecked checkbox whose name is a PHP array with an incrementing index will make a new array entry and set it to the boolean false. In fact, I think this is what it will do Unchecked checkboxes are never reported, they are left out of the request entirely. PHP will only know about the ones that were checked, and their values will be whatever the value attribute is. There won't be any empty elements filling in for unchecked boxes. Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted December 5, 2011 Share Posted December 5, 2011 Unchecked checkboxes are never reported, they are left out of the request entirely. PHP will only know about the ones that were checked, and their values will be whatever the value attribute is. There won't be any empty elements filling in for unchecked boxes. Thanks for the clarification, I usually wind up making the index's named for the ID and allow them to be set to checked if they were check to avoid the issue entirely since I can never remember. Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 6, 2011 Author Share Posted December 6, 2011 Thanks guys, this helped a lot. Quote Link to comment 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.