dannyb785 Posted April 16, 2013 Share Posted April 16, 2013 Hello, I seem to have had bad luck in getting any helpful answers for my last 2 threads so if you don't want to help, I'd rather you just not reply, but here goes: There are times when I need a list of users to be displayed and then a set of checkboxes next to them, where each checkbox represents something different. As an example, an admin page wants a list of users in a table and in each row, there's a checkbox for "admin"(to choose if that user is an admin), "suspended"(if that user is suspended), and so on. I initially thought that I would just scan each row and those that were checked, it would mark that value in the user table to 1(indicating that it was checked), but then I don't know how to detect a checkbox that was left unchecked. Sorry if my question doesn't make sense, so if it doesn't, I guess an easier way to ask is: how do you detect check boxes that weren't checked? Quote Link to comment https://forums.phpfreaks.com/topic/277019-how-to-handle-situation-with-multiple-checkboxes-on-multiple-rows/ Share on other sites More sharing options...
mac_gyver Posted April 16, 2013 Share Posted April 16, 2013 use an array for each check box. the array name is purpose of the check box and the array index is something that IDentifies who the check box is for, such as a user id - name='admin[1]' name='suspended[1]' name='admin[2]' name='suspended[2]' name='admin[3]' name='suspended[3]' the "checked" boxes will be the index values in the arrays. loop though the submitted arrays or use array_keys to extract the index values. Quote Link to comment https://forums.phpfreaks.com/topic/277019-how-to-handle-situation-with-multiple-checkboxes-on-multiple-rows/#findComment-1425144 Share on other sites More sharing options...
lemmin Posted April 16, 2013 Share Posted April 16, 2013 The browser isn't going to send the information if there is no value. The best way to make up for this would be to include the logic for the expected total checkboxes in your PHP. You could also trick the browser into sending the information by setting a "default" value for the variable in a hidden input: <input type="hidden" name="test[0]" value=""/> <input type="checkbox" name="test[0]" value="1"/> <input type="hidden" name="test[1]" value=""/> <input type="checkbox" name="test[1]" value="2"/> The second element with the same name will always override the variable's value. Again, I would recommend just including logic in your code so you don't have to rely so much on what the browser sends. Quote Link to comment https://forums.phpfreaks.com/topic/277019-how-to-handle-situation-with-multiple-checkboxes-on-multiple-rows/#findComment-1425145 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.