我试图从PHP查询填充的表中传递一个变量。将PHP变量传递给表中的JavaScript函数
在表的每一行上有三个按钮。一个是删除按钮,如果点击它将运行一个JavaScript警报,它将一个变量传递给另一个PHP页面,并从数据库中删除该页面。问题是当按钮被点击时,传递的变量始终是表格最后一行的变量。不是被点击的行。
代码:
<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>
如果你使用jQuery,你可以填充一个'data'字段并在你的按钮上使用它来触发一个特殊的链接。这通常比管理每个按钮更容易管理。 – tadman
onclick =“myFunction(<?php echo $ row [”recordnumber“];?>)”get tha value in function and set js itself function myFunction(recordnum){ –
你明白了吗? @劳伦斯辣椒 –