php php中通过POST多个同名输入

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

Multiple inputs with same name through POST in php

phphtmlarraysformspost

提问by Adam

Is it possible to get multiple inputs of the same name to post and then access them from PHP? The idea is this: I have a form that allows the entry of an indefinite number of physical addresses along with other information. If I simply gave each of those fields the same name across several entries and submitted that data through post, would PHP be able to access it?

是否可以将多个同名输入发布,然后从 PHP 访问它们?这个想法是这样的:我有一个表格,允许输入无限数量的物理地址和其他信息。如果我只是在多个条目中为每个字段指定相同的名称并通过 post 提交该数据,PHP 是否能够访问它?

Say, for example, I have five input on one page named "xyz" and I want to access them using PHP. Could I do something like:

比如说,我在一个名为“xyz”的页面上有五个输入,我想使用 PHP 访问它们。我可以做这样的事情:

    $_POST['xyz'][0]

If so, that would make my life ten times easier, as I could send an indefinite amount of information through a form and get it processed by the server simply by looping through the array of items with the name "xyz".

如果是这样,那将使我的生活轻松十倍,因为我可以通过表单发送无限量的信息,并且只需通过循环遍历名称为“xyz”的项目数组就可以让服务器对其进行处理。

回答by Eric

Change the names of your inputs:

更改输入的名称:

<input name="xyz[]" value="Lorem" />
<input name="xyz[]" value="ipsum"  />
<input name="xyz[]" value="dolor" />
<input name="xyz[]" value="sit" />
<input name="xyz[]" value="amet" />

Then:

然后:

$_POST['xyz'][0] == 'Lorem'
$_POST['xyz'][4] == 'amet'


If so, that would make my life ten times easier, as I could send an indefinite amount of information through a form and get it processed by the server simply by looping through the array of items with the name "xyz".

如果是这样,那将使我的生活轻松十倍,因为我可以通过表单发送无限量的信息,并且只需通过循环遍历名称为“xyz”的项目数组就可以让服务器对其进行处理。

Note that this is probably the wrong solution. Obviously, it depends on the data you are sending.

请注意,这可能是错误的解决方案。显然,这取决于您发送的数据。

回答by aziz punjani

In your html you can pass in an array for the name i.e

在您的 html 中,您可以为名称传入一个数组,即

<input type="text" name="address[]" /> 

This way php will receive an array of addresses.

这样 php 就会收到一个地址数组。

回答by Adam

Eric answer is correct, but the problem is the fields are not grouped. Imagine you have multiple streets and cities which belong together:

埃里克的回答是正确的,但问题是字段没有分组。想象一下,您有多个属于同一类的街道和城市:

<h1>First Address</h1>
<input name="street[]" value="Hauptstr" />
<input name="city[]" value="Berlin"  />

<h2>Second Address</h2>
<input name="street[]" value="Wallstreet" />
<input name="city[]" value="New York" />

The outcome would be

结果将是

$POST = [ 'street' => [ 'Hauptstr', 'Wallstreet'], 
          'city' => [ 'Berlin' , 'New York'] ];

To group them by address, I would rather recommend to use what Eric also mentioned in the comment section:

要按地址对它们进行分组,我宁愿建议使用 Eric 在评论部分中也提到的内容:

<h1>First Address</h1>
<input name="address[1][street]" value="Hauptstr" />
<input name="address[1][city]" value="Berlin"  />

<h2>Second Address</h2>
<input name="address[2][street]" value="Wallstreet" />
<input name="address[2][city]" value="New York" />

The outcome would be

结果将是

$POST = [ 'address' => [ 
                 1 => ['street' => 'Hauptstr', 'city' => 'Berlin'],
                 2 => ['street' => 'Wallstreet', 'city' => 'New York'],
              ]
        ]

回答by Antony

For anyone else finding this - its worth noting that you can set the key value in the input name. Thanks to the answer in POSTing Form Fields with same Name Attributeyou also can interplay strings or integers without quoting.

对于发现此问题的任何其他人 - 值得注意的是,您可以在输入名称中设置键值。感谢POSTing Form Fields with same Name Attribute 中的答案,您还可以在不引用的情况下交互字符串或整数。

The answers assume that you don't mind the key value coming back for PHP however you can set name=[yourval](string or int) which then allows you to refer to an existing record.

答案假设您不介意返回 PHP 的键值,但是您可以设置name=[yourval](字符串或整数),然后允许您引用现有记录。

回答by Azeemunnisa

It can be:

有可能:

echo "Welcome".$_POST['firstname'].$_POST['lastname'];