php 发布具有相同名称属性的表单字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2203430/
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 05:29:54  来源:igfitidea点击:

POSTing Form Fields with same Name Attribute

phppost

提问by danwellman

If you have a form containing text inputs with duplicate nameattributes, and the form is posted, will you still be able to obtain the values of all fields from the $_POSTarray in PHP?

如果您有一个包含具有重复name属性的文本输入的表单,并且该表单已发布,您是否仍然能够$_POST在 PHP 中从数组中获取所有字段的值?

回答by Gordon

No. Only the last input element will be available.

否。只有最后一个输入元素可用。

If you want multiple inputs with the same name use name="foo[]"for the input name attribute. $_POSTwill then contain an array for foo with all values from the input elements.

如果您想要多个具有相同名称的输入,请使用name="foo[]"输入名称属性。$_POST然后将包含一个 foo 数组,其中包含来自输​​入元素的所有值。

<form method="post">
    <input name="a[]" value="foo"/>
    <input name="a[]" value="bar"/>
    <input name="a[]" value="baz"/>
    <input type="submit" />
</form>

See the HTML reference at Sitepoint.

请参阅Sitepoint 上HTML 参考

The reason why $_POSTwill only contain the last value if you don't use []is because PHP will basically just explode and foreach over the raw query string to populate $_POST. When it encounters a name/value pair that already exists, it will overwrite the previous one.

$_POST如果您不使用,则只包含最后一个值的原因[]是因为 PHP 基本上只会爆炸并在原始查询字符串上 foreach 来填充$_POST。当它遇到一个已经存在的名称/值对时,它会覆盖前一个。

However, you can still access the raw query string like this:

但是,您仍然可以像这样访问原始查询字符串:

$rawQueryString = file_get_contents('php://input'))

Assuming you have a form like this:

假设你有一个这样的表格:

<form method="post">
    <input type="hidden" name="a" value="foo"/>
    <input type="hidden" name="a" value="bar"/>
    <input type="hidden" name="a" value="baz"/>
    <input type="submit" />
</form>

the $rawQueryStringwill then contain a=foo&a=bar&a=baz.

rawQueryString然后$将包含a=foo&a=bar&a=baz.

You can then use your own logic to parse this into an array. A naive approach would be

然后,您可以使用自己的逻辑将其解析为数组。一种天真的方法是

$post = array();
foreach (explode('&', file_get_contents('php://input')) as $keyValuePair) {
    list($key, $value) = explode('=', $keyValuePair);
    $post[$key][] = $value;
}

which would then give you an array of arrays for each name in the query string.

然后将为查询字符串中的每个名称提供一个数组数组。

回答by Daniel Mihai

Instead of name="nfo[val]"just use name="nfo[val][]"and in PHP you can use a foreach()

而不是name="nfo[val]"仅仅使用name="nfo[val][]"和在 PHP 中,您可以使用foreach()

HTML code:

HTML代码:

<form method="post">
    <input name="nfo[val][]" value="val1"/>
    <input name="nfo[val][]" value="val2"/>
    <input name="nfo[val][]" value="val3"/>
    <input type="submit" />
</form>

PHP code:

PHP代码:

$output='';
foreach ($nfo['val'] as $key=>$val) {
    $output.= $val.", ";
}

$outputwill be: val1, val2, val3

$output将会: val1, val2, val3

Hope this helps!

希望这可以帮助!

回答by streetparade

You have to create an array of them: with name=inputname[]and get with post,request or get

您必须创建一个数组:withname=inputname[]和 get with post、request 或 get

$inputs = $_POST['inputname'];

print_r($inputs);

回答by jpabluz

Only if the name are array-typed names[]in that case you will be getting an array as the variable in the $_POST variable.

只有names[]在这种情况下名称是数组类型时,您才会在 $_POST 变量中获得一个数组作为变量。