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>
}
?>
3

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

unable to call js function deleteEntry(id) from while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.