I have a table with the following PHP code:
<table id="myTable">
<tr class="header">
<th>Naam</th>
<th>Bedrijf</th>
</tr>
<?php
include 'assets/scripts/connect.php';
$q = mysql_query("SELECT * FROM `users` WHERE `admin`='0' AND `deleted`='0'");
while ($row = mysql_fetch_assoc($q)) {
$id = $row['id'];
?>
<tr class="trash" onclick="deletePopUp()" data-id="<?php echo $id; ?>">
<td><?php echo $row['fname']; ?> <?php echo $row['lname']; ?></td>
<td><?php echo $row['company']; ?></td>
</tr>
<?php
}
?>
</table>
As you can see, I add a data-id for each row that is created by the code in conjunction with the database. The onClick function makes a modal pop up with the request to confirm whether the user really wants to delete this customer. The modal is coded the following way:
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-top">
<h1>Weet je het zeker?</h1>
<span class="close">×</span>
</div>
<div class="modal-inner">
<a href="delete-user.php?id=<?php echo $id; ?>" id="delete" class="btn-confirm">Verwijderen</a>
<a href="#" class="btn-cancel cancel">Annuleren</a>
</div>
</div>
</div>
The two anchor tags are for the confirmation. So one sould go to the external PHP script to delete the customer according to their ID. I use the GET method for this. But the link on the modal now forms always with the same ID. Do the link is always:
/assets/scripts/delete-user.php?id=2
I would like to have the ID being the same as that of the customer clicked on.
I tried using jQuery to form this link, but I get the same result. This is the jQuery I used:
var id=$('.trash').data('id');
$('#delete').attr('href','/assets/scripts/delete-user.php?id='+id);
But as said, this gives the same result.
How can I get the link on the modal to have a dynamic ID?
mysql_*functions. They have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use the mysqli_* or PDO functions with prepared statements and bound parameters.