php中通过POST多个同名输入 [英] Multiple inputs with same name through POST in php

查看:22
本文介绍了php中通过POST多个同名输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如,我在一个名为xyz"的页面上有五个输入,我想使用 PHP 访问它们.我可以做类似的事情:

 $_POST['xyz'][0]

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

解决方案

更改输入的名称:

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

那么:

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

<小时><块引用>

如果是这样,那将使我的生活轻松十倍,因为我可以发送通过表格获取无限量的信息并对其进行处理由服务器简单地通过循环遍历项目数组命名xyz".

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

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?

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:

    $_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".

解决方案

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".

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

这篇关于php中通过POST多个同名输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆