简体   繁体   English

如何使用$ _GET在PHP中使用相同的名称获取多个参数

[英]How to use $_GET to get multiple parameters using the same name in PHP

I'm using checkboxes to search a mysql database I have created. 我正在使用复选框来搜索我创建的mysql数据库。 As the checkboxes all use the same name, when I use $_GET, it only gets the last value in the URL. 由于复选框都使用相同的名称,当我使用$ _GET时,它只获取URL中的最后一个值。

For example: http://www.website.com/search.php?features=Textures&features=Items&style=Realistic&submit=search 例如: http//www.website.com/search.php?feature = Text&feature = Items&style = Realistic&subsmit = search

would only return Items, and would override Textures. 只会返回Items,并会覆盖Textures。

Is there a way to store both the values, and then to use these values to search my database? 有没有办法存储这两个值,然后使用这些值来搜索我的数据库?

PHP is a little odd here. PHP在这里有点奇怪。 Using its standard form data parser, you must end the name of the controls with [] in order to access more than one of them. 使用其标准表单数据解析器,您必须使用[]结束控件的名称,以便访问多个控件的名称。

<input type="checkbox" name="foo[]" value="bar">
<input type="checkbox" name="foo[]" value="bar">
<input type="checkbox" name="foo[]" value="bar">

Will be available as an array in: 将作为数组提供:

$_GET['foo'][]

If you don't want to rename the fields, then you will need to get access to the raw data ( $_SERVER['REQUEST_URI'] ) and parse it yourself (not something I'd recommend). 如果您不想重命名字段,那么您将需要访问原始数据( $_SERVER['REQUEST_URI'] )并自行解析(不是我推荐的)。

I have never tried it with $_GET, but if you use $_POST you can do something like this: 我从未尝试使用$ _GET,但如果你使用$ _POST,你可以这样做:

<input type="checkbox" name="car[]" value="honda" /> Honda
<input type="checkbox" name="car[]" value="ford" /> Ford
<input type="checkbox" name="car[]" value="toyota" /> Toyota
// note the [] in the name

so that $car = $_POST['car'] is an array 所以$ car = $ _POST ['car']是一个数组

You can try it with $_GET as well and see. 您也可以使用$ _GET尝试并查看。

Name your checkbox elements "features[]" in html. 在html中将复选框元素命名为“features []”。 That way they will be passed as an array. 这样它们将作为数组传递。

你需要为该变量创建一个HTML数组变量的NAME,这样就可以了

<input name='features[]'/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM