所以我有一個看起來像這樣的 php 頁面

<?php
echo "<table border='1' width= 300px >
<tr>
<th>Friend Names</th>
<th>Remove Friends</th>
</tr>";
while($row = mysqli_fetch_assoc($result2))
{
$friend_id_got = $row['friend_id2'];
$query3 = "SELECT profile_name
from friends
where friend_id = '$friend_id_got' ";
$result3 = $conn->query($query3);
$final3 = mysqli_fetch_assoc($result3);
echo "<tr>";
echo "<td>" . $final3['profile_name'] . "</td>";
echo "<td>"
?>
<form action="friendlist.php" method= "POST">
<button id="add-friend-btn" type= 'submit' name = 'submit'>Unfriend</button>
</form>
<?php
"</td>";
echo "</tr>";
}
echo "</table>";
When I press the button, I need the corresponding name to delete it. The only problem I'm facing is that How do I get the name corresponding to the button. I think we need to relate the buttons and the name somehow, so when a specific button is pressed i get the corresponding name
uj5u.com熱心網友回復:
從問題和評論中,再看一眼您的代碼,聽起來您可能實際上需要在單擊按鈕時將兩條資料提交給服務器:
回應請求應采取的行動(即取消好友關系)
被取消好友的人的ID
為此,您可以在表單中添加一些隱藏欄位。這些對用戶不可見,但在$_POST提交表單時可用于資料中的PHP 。
像這樣的東西:
<form action="friendlist.php" method= "POST">
<input type="hidden" name="unfriend_id" value="<?=$friend_id_got ?>" />
<input type="hidden" name="action" value="unfriend" />
<button id="add-friend-btn" type="submit" name= "submit">Unfriend</button>
</form>
uj5u.com熱心網友回復:
繼@ADyson 的評論之后:
<form action="friendlist.php" method= "POST">
<input type="hidden" name="cancel_id" value="<?=$friend_id_got ?>" />
<button id="add-friend-btn" type="submit" name="submit">Unfriend</button>
</form>
通過在表單中??包含一個隱藏欄位,您可以存盤更多資訊。
您可以看到我在隱藏欄位的值中存盤了您取消好友的朋友的 ID,因此當提交表單(單擊按鈕)時,您將可以訪問 POST 資料中的“cancel_id” ,其中顯然包含要取消好友的好友的 ID。
uj5u.com熱心網友回復:
<!-- On top of your page -->
<?php
if(isset($_GET['friend_id'])){
$fid = $_GET['friend_id'];
$deleteAction = mysqli_query($your_connection_variable,"DELETE from friends where id='$fid'");
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
?>
<!-- ends -->
<table border='1' width=300px>
<tr>
<th>Friend Names</th>
<th>Remove Friends</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result2))
{
$friend_id_got = $row['friend_id2'];
$query3 = "SELECT profile_name
from friends
where friend_id = '$friend_id_got' ";
$result3 = $conn->query($query3);
$final3 = mysqli_fetch_assoc($result3);
?>
<tr>
<td><?=$final3['profile_name']?></td>
<td><a href="your_current_page_name.php?friend_id=<?php echo $final3['id'] ?>">Unfriend</a></td>
</td>
</tr>
<?php } ?>
</table>
無需添加表單來洗掉特定 id 只需傳遞該朋友的 id 并在同一頁面上點擊獲取請求(頁面可能會有所不同,根據您的需要)。現在獲取 id 并執行與該行的洗掉查詢對應
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329260.html
下一篇:將負數更改為正數不起作用
