gpt4 book ai didi

javascript - 将 PHP 变量传递给表中的 JavaScript 函数

转载 作者:行者123 更新时间:2023-12-02 14:26:10 28 4
gpt4 key购买 nike

我正在尝试从由 PHP 查询填充的表传递变量。

表格的每一行都有三个按钮。其中一个是删除按钮,如果单击该按钮,它将运行 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>

最佳答案

while($row = mysqli_fetch_assoc($result)) 循环多次声明 javascript 函数myFunction,且全部具有相同的名称。像这样

<?php while($row = mysqli_fetch_assoc($result)) { ?>
<script>
function myFunction() {} // this function name is repeated in the while loop
<script>
<?php } ?>

函数不能具有相同的名称。最简单的解决方案是使用不同的名称。

<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<?php echo $row["recordnumber"];?>()" // <-- edited line
data-toggle="tooltip" title="Delete Prefix"
>
<i class="fa fa-close"></i>
<script>
function myFunction<?php echo $row["recordnumber"];?>() { // <-- edited line
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>

更好且简单的解决方案是使用 recordnumber 作为函数参数

<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(<?php echo $row["recordnumber"];?>)"
data-toggle="tooltip" title="Delete Prefix"
>
<i class="fa fa-close"></i>
<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>

将 javascript 函数放在 while 循环之外的位置,以便它不会重复

<script>
function myFunction(recordnumber) {
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=recordnumber';
});
}
</script></a>

关于javascript - 将 PHP 变量传递给表中的 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38210100/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com