如何在PHP中传递多个隐藏输入的当前行的一个值。我有以下代码:
foreach($portfolio as $portfolio){
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</td>";
echo "</tr>";
}
我也有触发表单的每一行的提交按钮。当我单击提交按钮时,它会提交隐藏输入行的所有值。我只想要点击的按钮行值。
网址如下:
/portfolio?portfolio_id=1&portfolio_id=2&portfolio_id=3&portfolio_id=4
依此类推
我只想要
/portfolio?portfolio_id=3
答案 0 :(得分:2)
每行都有一个新表格......
<form method="get">
</form>
像这样:
foreach($portfolio as $portfolio){
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo '<form method="get">';
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</form>";
echo "</td>";
echo "</tr>";
}
答案 1 :(得分:0)
表单标记应包含在循环中。
更改此
foreach ($portfolio as $portfolio) {
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</td>";
echo "</tr>";
}
为:
foreach ($portfolio as $portfolio) {
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo "<form action='process.php' method='get'>";
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</form>";
echo "</td>";
echo "</tr>";
}
答案 2 :(得分:0)
如果您提交的内容都是该ID,并且您使用的是GET
而不是POST
,那么您甚至可以不使用表单而是使用链接。如果需要,您仍然可以将链接设置为看起来像按钮,但使用链接会在语义上更有意义。删除隐藏的输入,并让每个链接都有您想要的URL。