使用PHP获取具有相同名称的2个HTML输入标签的值 [英] Get the values of 2 HTML input tags having the same name using PHP

查看:108
本文介绍了使用PHP获取具有相同名称的2个HTML输入标签的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有下面的表:

Suppose I have the following table:

<form  action="bla.php" method=post>
<table class="pv-data">
<tr>
<td><input type="text" name="id" size="2" value=1 /></td>
<td><input type="text" name="longitude" size="7"/></td>
<td><input type="text" name="latitude" size="7"/></td>
</tr>
<tr>
<td><input type="text" name="id" size="2" value=2 /></td>
<td><input type="text" name="longitude" size="7"/></td>
<td><input type="text" name="latitude" size="7"/></td>
</tr>
</table>
<input type="submit" name="submit" value="SUBMIT">
</form>

我想将此表中的值赋给php变量,所以在如果我使用 $ id = $ _REQUEST ['id'];

I want to assign the values in this table to php variables, so in bla.php

c>只有最后一个值被采用,因为有两个输入标签具有相同的名称。

if I used $id = $_REQUEST['id']; only the last value is taken because there are two input tags that have the same name.

因此,我可以请求这些具有相同名称的标签的2个值PHP?

So can I request the 2 values of these tags having the same name into php?

ps不会告诉我更改输入标记的名称,因为表格比这个更复杂一点,它具有动态添加行:
我的真实代码是: http://jsfiddle.net/CchES/9/

p.s don't tell me to change the name of the input tags, because the table is bit more complicated than that and it has dynamic add rows: My real code is: http://jsfiddle.net/CchES/9/

推荐答案

当您重复名称时,您必须为它们提供数组样式名称:

When you have repeated names, you have to give them array-style names:

<form  action="bla.php" method=post>
<table class="pv-data">
<tr>
<td><input type="text" name="id[]" size="2" value=1 /></td>
<td><input type="text" name="longitude[]" size="7"/></td>
<td><input type="text" name="latitude[]" size="7"/></td>
</tr>
<tr>
<td><input type="text" name="id[]" size="2" value=2 /></td>
<td><input type="text" name="longitude[]" size="7"/></td>
<td><input type="text" name="latitude[]" size="7"/></td>
</tr>
</table>
<input type="submit" name="submit" value="SUBMIT">
</form>

当您这样做时, $ _ POST ['id'] $ _ POST ['latitude'] $ _ POST ['longitude'] 包含值。

When you do this, $_POST['id'], $_POST['latitude'], and $_POST['longitude'] will be arrays containing the values.

您的表单处理代码可以遍历这些:

Your form processing code can then iterate over these:

for ($i = 0; $i < count($_POST['id']); $i++) {
  if (isset($_POST['latitude'][$i], $_POST['longitude'][$i])) { // Make sure both are filled in
    // Do stuff with this row of the form
  }
}

这篇关于使用PHP获取具有相同名称的2个HTML输入标签的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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