以下代码我使用输入名称result + row id及其按预期方式工作。
<td class="t-width-150" id="result">
<input type="text" name="result<?php echo $row['id']; ?>" id="txtresult" value="0">
</td>
现在,我想在单击“提交”按钮后在另一个页面中获得此ID和ID。我应该在以下代码中使用什么:
<td class="t-width-150">
<?php echo $_POST['result']; ?>
</td>
谢谢!
更新:我已解决。不需要更多答案。谢谢大家!
答案 0 :(得分:1)
您可以使用如下所示的表单字段命名格式:
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";
}
答案 1 :(得分:0)
您是在说您不预先知道会有什么POST变量,因为表单中存在未知数量的结果,因此未知数量的文本框吗?如果是这样,那么您可以
a)循环浏览所有$_POST
变量,以找到所有以“结果”开头的变量,然后从中提取值,例如
foreach (array_keys($_POST) as $key)
{
if (preg_match("/result(\d+)/", $key, $matches))
{
echo $_POST[$key]; //will get the value entered in the input field
echo $matches[1]; //will get you the row ID from the field name e.g. from "result123" it would return "123".
}
}
或
b)在表单字段名称中使用数组语法,并将结果字段和ID字段分开,例如
<input type="text" name="result[]" id="txtresult" value="0" />
<input type="hidden" name="rowID[]" value="<?php echo $row['id']; ?>"/>
然后在PHP中,您将遍历$_POST["result"]
,然后检查$_POST["rowID"]
中的匹配索引,该索引将是数组,以将结果关联在一起。