我有一个表格,每行都有一个按钮来查看记录,当我点击该按钮时,我想发送该行的记录ID并将其存储在变量中。
这是我到目前为止所拥有的
foreach($result as $row)
{
echo "<td> <form method='post'><input name='viewticket' type='submit' id='viewticket' value='View Ticket' class='btn btn-xs btn-danger'/></form></td>";
echo "<td>".$row['isopen']."</td>";
echo "<td>".$row['title']."</td>";
echo "<td>".$row['id']."</td> </tr>";
}
if($_POST['viewticket']){
$theid= //not sure how it would work
}
以下是用户界面的外观:
答案 0 :(得分:2)
您可以插入
"<input type='hidden' name='selectedID' value='" + $row['id'] + "'/>"
进入表单,然后你有$ _POST ['selectedID']中的ID:
foreach($result as $row)
{
echo "<td> <form method='post'><input name='viewticket' type='submit' id='viewticket' value='View Ticket' class='btn btn-xs btn-danger'/>";
echo "<input type='hidden' name='selectedID' value='" + $row['id'] + "'/></form></td>";
echo "<td>".$row['isopen']."</td>";
echo "<td>".$row['title']."</td>";
echo "<td>".$row['id']."</td> </tr>";
}
}
if($_POST['viewticket']){
echo $_POST['selectedID'];
}
答案 1 :(得分:0)
另一种选择是使用GET请求而不是POST。
所以,你的代码看起来像这样:
foreach($result as $row) {
?>
<td>
<a href="?id=<?php echo $row['id'] ?>">
<input name='viewticket' type='submit' id='viewticket' value='View Ticket' class='btn btn-xs btn-danger'/>
</a>
</td>
<td><?php echo $row['isopen'] ?></td>
<td><?php echo $row['title'] ?></td>
<td><?php echo $row['id'] ?></td>
<?php
}
if(isset($_GET['id'])){
$theid = $_GET['id'];
}