I am trying to pass a variable from a table that was populated by a PHP query.
On each row of the table there are three buttons. One is a delete button that if clicked it will run a JavaScript alert which passes a variable to another PHP page where it is deleted from the database. The problem is when the button is clicked the variable passed is always the variable from the last row of the table. Not the row being clicked.
The code:
<table class="table table-bordered table-striped js-dataTable-full-pagination">
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td class="text-center"><a href="/record.php?report=<?php echo $row['recordnumber']; ?>"><?php echo $row['recordnumber']; ?></a></td>
<td class="font-w600"><?php echo $row["description"];?></td>
<td class="hidden-xs"><?php echo $row["type"];?></td>
<td class="hidden-xs"><?php echo $row["user"];?></td>
<td class="hidden-xs"><?php echo $row["edate"];?></td>
<td class="text-center">
<a class="btn btn-xs btn-primary" href="/updaterecordform.php?id=<?php echo $row["recordnumber"];?>" data-toggle="tooltip" title="Edit Record"><i class="fa fa-pencil"></i></a>
<a class="btn btn-xs btn-danger" onclick="myFunction()" data-toggle="tooltip" title="Delete Prefix"><i class="fa fa-close"></i><script>
function myFunction() {
swal({
title: 'Are you sure?',
text: 'You will not be able to recover this Record!',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#d26a5c',
confirmButtonText: 'Yes, delete it!',
closeOnConfirm: false,
html: false
}, function () {
window.location.href = 'deleterecord.php?id=<?php echo $row['recordnumber']; ?>'
});
}
</script></a>
<a class="btn btn-xs btn-primary" href="/record.php?id=<?php echo $row["recordnumber"];?>" data-toggle="tooltip" title="View Details"><i class="fa fa-list-ul"></i></a>
</td>
</tr>
<?php } ?>
</table>
datafield and use that on your button to trigger a special link. That's usually a lot easier to manage than to write a script per button.