如何区分两个同名的多选列表 [英] How to distinguish between two multiple selection lists with same name

查看:111
本文介绍了如何区分两个同名的多选列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在构建的系统,我需要查看哪些选项来自第一个列表(带有POST请求),以及哪些选项来自第二个列表。

 < form method =post> 
< select name =cars []multiple>
< option selected> test< / option>
< option selected> test2< / option>
< option> test3< / option>
< option> test4< / option>
< / select>
< select name =cars []multiple>
< option> hai< / option>
< option> hai2< / option>
< option selected> hai3< / option>
< option selected> hai4< / option>
< / select>
< input type =submit>
< / form>

现在他们进来了:

 数组

[cars] =>数组

[0] => test
[ 1] => test2
[2] => hai3
[3] => hai4


/ pre>

实际上,我想从'haiX'选项中检索'testX'选项。现在我看不到第一个列表中的哪一个来自第二个列表。当然,我知道我也可以使第二个列表的名称不同于第一个列表的名称,但是我想这样做是有原因的。

解决方案

通过以下形式创建多维数组来分类:

 < ; form method =post> 
< select name =cars [0] []multiple>
< option selected> test< / option>
< option selected> test2< / option>
< option> test3< / option>
< option> test4< / option>
< / select>
< select name =cars [1] []multiple>
< option> hai< / option>
< option> hai2< / option>
< option selected> hai3< / option>
< option selected> hai4< / option>
< / select>
< input type =submit>
< / form>

然后像这样读取它:$ _POST ['cars'] [0] for the first set和$ _POST ['cars'] [1]为第二个$ / b $ b $

For a system I'm building I need to see which options came in (with POST request) from the first list and wich options came in from the second list.

<form method="post">
    <select name="cars[]" multiple>
        <option selected>test</option>
        <option selected>test2</option>
        <option>test3</option>
        <option>test4</option>
    </select>
    <select name="cars[]" multiple>
        <option>hai</option>
        <option>hai2</option>
        <option selected>hai3</option>
        <option selected>hai4</option>
    </select>
    <input type="submit">
</form>

Now they're coming in like this:

Array
(
    [cars] => Array
        (
            [0] => test
            [1] => test2
            [2] => hai3
            [3] => hai4
        )
)

Actually I want to retrieve the 'testX' options apart from the 'haiX' options. Now I can't see which came from the first list and wich from the second. Off course I know I could also make the name of the second list different than the name of the first list, but it's for good reason that I want to do it like this..

解决方案

Divide the categories by creating a multidimensional array in the form:

<form method="post">
    <select name="cars[0][]" multiple>
        <option selected>test</option>
        <option selected>test2</option>
        <option>test3</option>
        <option>test4</option>
    </select>
    <select name="cars[1][]" multiple>
        <option>hai</option>
        <option>hai2</option>
        <option selected>hai3</option>
        <option selected>hai4</option>
    </select>
    <input type="submit">
</form>

And then reading it like this: $_POST['cars'][0] for the first set and $_POST['cars'][1] for the second

这篇关于如何区分两个同名的多选列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆