So I have a php page that looks like this
<?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
CodePudding user response:
From the question and comments, and a glance at your code it sounds like you probably actually need two pieces of data to be submitted to the server when your button is clicked:
The action which should be undertaken in response to the request (i.e. unfriending)
The ID of the person being unfriended
To achieve that you can add some hidden fields to your form. These are invisible to the user but will be available to PHP in the $_POST
data when the form is submitted.
Something like this:
<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>
CodePudding user response:
Following on from the comment from @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>
By including a hidden field in the form, you're able to store more information.
You can see that I'm storing the ID of the friend you're unfriending in the value of the hidden field, so when the form is submitted (the button is clicked) you'll have access to "cancel_id" in the POST data, which will obviously contain the ID of the friend to unfriend.
CodePudding user response:
<!-- 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>
No Need to add a form to delete a specific id just pass the id of that friend and hit a get request on same page (Page my be different it's as per your need). Now fetch the id and execute delete query correspondence to that row