php 从多选值中获取所有 $_POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13110758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:47:36  来源:igfitidea点击:

Getting All $_POST From Multiple Select Value

phphtmlpost

提问by Saint Robson

I have this HTML code :

我有这个 HTML 代码:

<select name="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

and to make user able to select more than one items, I'm using this jQuery plugin : http://harvesthq.github.com/chosen/

为了让用户能够选择多个项目,我使用了这个 jQuery 插件:http: //harvesthq.github.com/chosen/

but, once it submitted... the PHP script only able to retrieve one of $_POST['cars']value. the last one. how to make PHP to be able to retrieve ALL of it?

但是,一旦它提交... PHP 脚本只能检索$_POST['cars']值之一。最后一个。如何让 PHP 能够检索所有它?

回答by Saint Robson

I've found the answer...

我找到了答案...

<select name="cars[]" multiple="multiple">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

and in the PHP part :

在 PHP 部分:

$cars = $_POST['cars'];
print_r ($cars);

回答by Alaa M. Jaddou

you must do the following:

您必须执行以下操作:

//$_POST or $_GET is the method of your form request

//$_POST 或 $_GET 是你的表单请求的方法

foreach ($_POST['cars'] as $selected_option) {
    echo $selected_option;
}

that's it.

就是这样。