views:

316

answers:

2

Alright, let me start with an example. I have a bunch of items and they can fall into series of categories. An item can be in multiple categories. Basically what I have is a listing of items with checkboxes grouped by category. Again an item can appear more than once. So it could look something like:

Blue Items: Item 1 Item 2 Item 3

Red Items: Item 1 Item 4

So next to each item there is a checkbox to delete the item-category association. What should I store in the value of the input so I can differentiate items between categories? I mean I can't use the item number because it can appear in multiple categories. I could do something like "blue-item2" then when going through the form with my server side script split the value string with "-" but that seems a little iffy.

Sorry if the question is a little vague. I can clarify a little if need be.

+6  A: 

A form containing these elements:

<input type="checkbox" name="item[blue][0]">
<input type="checkbox" name="item[blue][1]">
<input type="checkbox" name="item[blue][2]">

<input type="checkbox" name="item[red][0]">
<input type="checkbox" name="item[red][1]">

will result in the following value for the $_POST['item'] element:

array(
    'blue' => array(0, 1, 2),
    'red'  => array(0, 1),
);
Ionuț G. Stan
that be the way to do it, arrr
pxl
+1  A: 

you can make use of the name tag for each checkbox, and give them the same name for same category. Like say you have a checkbox set for blue items then input type="check" name="blueitems[]"

do you notice the [] (array) in the name, that is to provide same name across the same group. Now this way you can access document.getElementsByName["blueitems[]"], you will be provided with an array, and using an index you can access the particular element.

a similar example can be found here. Example for using name arrays

Anirudh Goel