dongqigu0429 2013-06-27 13:22
浏览 27
已采纳

使用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>

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

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

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

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/

  • 写回答

4条回答 默认 最新

  • douyi5822 2013-06-27 13:27
    关注

    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>
    

    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
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程