开发者

Multiple values for an input in a form?

开发者 https://www.devze.com 2022-12-09 00:29 出处:网络
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 chec

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 c开发者_开发问答ategory. 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.


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),
);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消