首页 > 解决方案 > 单击提交按钮后如何使用 id 获取此名称?

问题描述

下面的代码我使用输入名称结果+行 id 并且它按预期工作。

<td class="t-width-150" id="result">
   <input type="text" name="result<?php echo $row['id']; ?>" id="txtresult" value="0">
</td>

现在我想在单击提交按钮后在另一个页面中获取这个带有 id 的名称。我应该在下面的代码中使用什么:

<td class="t-width-150">
  <?php echo $_POST['result']; ?>
</td>

谢谢!

更新:我解决了。不需要更多答案。谢谢大家!

标签: phphtmlsubmitform-submit

解决方案


您可以使用这样的表单字段命名格式:

name="result[<?php echo $row['id']; ?>]"

如此有效,这将导致字段名称如result[1], result[2], result[5](ID 不需要连续或按顺序排列。)

然后,PHP 将在其中生成一个数组$_POST['result'],您可以使用扩展的 foreach 语法循环访问该数组,以访问您的行 ID 和提交的值:

foreach( $_POST['result'] as $id => $value ) {
  echo "The submitted value for the record with ID ", $id, " was ", $value, "<br>\n";
}

推荐阅读