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

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

问题描述

时有可能得到相同名称的多个输入后,然后从PHP访问它们?我们的想法是这样的:我有一个表单,允许物理地址的数目不定与其他信息一起输入。如果我只是给每个这些领域的同名跨越多个条目,并提交通过post数据,将PHP能够访问它?

说,例如,我有一个名为XYZ在一个页面上五个输入,我想用PHP来访问它们。我可以这样做:

  $ _ POST ['XYZ'] [0]

如果是这样,这将让我的生活十倍更容易,因为我可以通过表单发送的信息无限期地得到它由服务器处理简单地通过一个名为XYZ项目的数组循环。


解决方案

更改您输入的名称:

 <输入名称=XYZ []值=排版/>
<输入名称=XYZ []值=文字/>
<输入名称=XYZ []值=悲/>
<输入名称=XYZ []值=坐/>
<输入名称=XYZ []值=阿梅特/>

然后:

  $ _ POST ['XYZ'] [0] =='的Lorem
$ _ POST ['XYZ'] [4] =='阿梅德



  

如果是这样,这将让我的生活十倍更容易,因为我可以寄
  通过表单的信息不定金额,把它处理
  通过简单地由通过与项目阵列循环服务器
  名为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天全站免登陆