简体   繁体   English

php中通过POST多个同名输入

[英]Multiple inputs with same name through POST in php

Is it possible to get multiple inputs of the same name to post and then access them from PHP?是否可以将多个同名输入发布,然后从 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?如果我只是在多个条目中为每个字段指定相同的名称并通过 post 提交该数据,PHP 是否能够访问它?

Say, for example, I have five input on one page named "xyz" and I want to access them using PHP.比如说,我在一个名为“xyz”的页面上有五个输入,我想使用 PHP 访问它们。 Could I do something like:我可以做这样的事情:

    $_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”的项目数组就可以让服务器对其进行处理。

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.显然,这取决于您发送的数据。

In your html you can pass in an array for the name ie在您的 html 中,您可以为名称传入一个数组,即

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

This way php will receive an array of addresses.这样 php 就会收到一个地址数组。

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'],
              ]
        ]

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 Attribute you 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] (字符串或整数),然后允许您引用现有记录。

有可能:

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

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

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