HTML表格使用GET进行多选 [英] HTML forms multi-select using GET

查看:69
本文介绍了HTML表格使用GET进行多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:

为此很难用Google搜索,因为"GET"带来了很多如何用javascript'获取'表单的值"等信息.

It's sort of hard to google for this, because "GET" is bringing up a lot of "how to 'get' the value of a form with javascript," etc.

问题:

TL; DR版本优先

您可以在表单的GET方法中使用多选功能,并且仍然检索每个单独的值吗?

扩展说明

我正在构建的可搜索目录的要求包括将GET方法与表单一起使用,以便用户可以在url中查看其结果,还可以将指向搜索结果的直接链接发送给其他客户,等等.

The requirements of a searchable catalog I'm building include using the GET method with the form, so that the user can see their results in the url, and also send a direct link to the search results to another customer, etc.

可搜索字段之一是多选框(具有MULTIPLE属性的选择框).我的后端是用PHP编写的,我通常通过将名称设置为数组变量(name="multiselect[]")来处理多选,而post变量包括所选选项($_POST["multiselect"] == array())的数组.

One of the searchable fields is a multi-select box (a select box with the MULTIPLE attribute). My back end is written in PHP, and I usually handle a multi-select by setting the name to an array variable (name="multiselect[]") and the post variable includes an array of the selected options ($_POST["multiselect"] == array()).

在这种情况下,问题是将多选值传递给get字符串,如下所示:

The problem in this case is that the multi-select values are passed to the get string like this:

action?multiselect=1&multiselect=2

因此,无论最后一个值是什么,都将替换get字符串中变量的首次初始化的值. (在上面的示例中,multiselect等于"2").

So whatever the last value is replaces the value of the first initialization of the variable in the get string. (in the above example, multiselect would equal "2").

尝试将名称命名为数组只会像这样用相同的方式替换数组值

Trying to make the name an array just makes the array value replaced in the same manner, like this

action?multiselect[]=1&multiselect[]=2

将得到$_GET["multiselect"] == 2

我最初建议使用一个复选框,因为它可以让我们以不同的方式命名元素并检查每个元素的true/false,但是此特定的多重选择大约有30个值,他们希望它是在可滚动区域中.

I had originally recommended using a checkbox, as it would allow us to name the elements differently and check for true/false on each one, but there are around 30 values for this particular multi-select, and they want it to be in a scroll-able area.

推荐答案

为我工作.刚刚尝试

http://www.nearby.org.uk/tmp/multi- test.html

<select name="multiselect[]" ...

产生一个URL ?multiselect%5B%5D = 2& multiselect%5B%5D = 3

Results in a URL ?multiselect%5B%5D=2&multiselect%5B%5D=3

这将使$ _GET ['multiselect']在PHP中成为数组.

which will make $_GET['multiselect'] an array in PHP.

也许您的系统中还有其他内容,剥夺了[]吗?

Perhaps you have something else in your system, stripping the [] ?

这篇关于HTML表格使用GET进行多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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