Jump to content

How to handle situation with multiple checkboxes on multiple rows


dannyb785

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.