0

I want to call a javascript function myfunction() from php code. 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
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>
}
?>
5ahil
  • 3
  • 3
  • look at [this](https://stackoverflow.com/questions/32666339/calling-javascript-function-in-php-while-passing-php-variables-into-the-function) – Max Sep 05 '17 at 03:51
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Teemu Sep 05 '17 at 03:55
  • is there any possible way to serve my purpose? help me out. – 5ahil Sep 05 '17 at 03:58

1 Answers1

1

Write another PHP script that deletes an entry by ID and call it from your web page. I.e.,

<?
while (...) {
    echo "<button onclick='deleteEntry($id)'></button>"
}
?>

With your JS being something like this:

function deleteEntry(id) {
    if (confirm('Are you sure?')) {
        fetch('myserver/delete_entry.php?id=' + id);
    }
}

You have to write delete_entry.php, of course.

Alex Undefined
  • 620
  • 1
  • 5
  • 8