[英]Call javascript function (which accept parameters) from php loop and process the parameters in javascript function
I want to call a javascript function myfunction() from php code. 我想从php代码中调用JavaScript函数myfunction() 。 the code is as follows: 代码如下:
<script>
function myfunction() {
var r = confirm("Are you sure ? ");
if (r == true) {
//delete file from database with id b_id
}
<?php header("Refresh:0"); ?>
}
</script>
I want to call the js function from this php code. 我想从此php代码调用js函数。
<?php
while(//fetched database elements one by one)
{
//$b_id fetched from database.
echo "<button class='w3-display-topright pe-7s-close-circle w3-xxlarge w3-button' onclick="myfunction($b_id);"></button>
}
?>
Write another PHP script that deletes an entry by ID and call it from your web page. 编写另一个PHP脚本,该脚本按ID删除条目并从您的网页中调用它。 Ie, 也就是说,
<?
while (...) {
echo "<button onclick='deleteEntry($id)'></button>"
}
?>
With your JS being something like this: 随着您的JS是这样的:
function deleteEntry(id) {
if (confirm('Are you sure?')) {
fetch('myserver/delete_entry.php?id=' + id);
}
}
You have to write delete_entry.php
, of course. 当然,您必须编写delete_entry.php
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.