How to Execute PHP function with onclick

0 votes

I am searching for a simple solution to call a PHP function only when a-tag is clicked.

PHP:

function removeday() { ... }

HTML:

<a href="" onclick="removeday()" class="deletebtn">Delete</a>

The html and PHP code are in the same PHP file.

Jun 16, 2020 in PHP by kartik
• 37,520 points
26,036 views

1 answer to this question.

0 votes

Hello @kartik,

In javascript, make an ajax function,

function myAjax() {
      $.ajax({
           type: "POST",
           url: 'your_url/ajax.php',
           data:{action:'call_this'},
           success:function(html) {
             alert(html);
           }

      });
 }

Then call from html,

<a href="" onclick="myAjax()" class="deletebtn">Delete</a>

And in your ajax.php,

if($_POST['action'] == 'call_this') {
  // call removeday() here
}

Hope it works!

answered Jun 16, 2020 by Niroj
• 82,800 points