Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute PHP function with onclick

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> 

UPDATE: the html and PHP code are in the same PHP file

like image 840
Mike Avatar asked Oct 11 '13 16:10

Mike


People also ask

Can you use onclick with PHP?

Use jQuery to Execute the PHP Function With the onclick() Event. We can use jQuery to execute the onclick() event by writing a function that will execute the PHP function. For example, create a PHP file echo.

How do you run a PHP function when a button is clicked?

Calling a PHP function using the HTML button: Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function called.

How can I call PHP function on button click in Wordpress?

You cannot directly call a php function using a button click. But you can call Js functions on button click event. And then you can implement Ajax to call php functions. You are using wordpress so you can use wp functions or custom coding to achieve this.


2 Answers

First, understand that you have three languages working together:

  • PHP: It only runs by the server and responds to requests like clicking on a link (GET) or submitting a form (POST).

  • HTML & JavaScript: It only runs in someone's browser (excluding NodeJS).

I'm assuming your file looks something like:

<!DOCTYPE HTML> <html> <?php   function runMyFunction() {     echo 'I just ran a php function';   }    if (isset($_GET['hello'])) {     runMyFunction();   } ?>  Hello there! <a href='index.php?hello=true'>Run PHP Function</a> </html> 

Because PHP only responds to requests (GET, POST, PUT, PATCH, and DELETE via $_REQUEST), this is how you have to run a PHP function even though they're in the same file. This gives you a level of security, "Should I run this script for this user or not?".

If you don't want to refresh the page, you can make a request to PHP without refreshing via a method called Asynchronous JavaScript and XML (AJAX).

That is something you can look up on YouTube though. Just search "jquery ajax"

I recommend Laravel to anyone new to start off right: http://laravel.com/

like image 180
Michael J. Calkins Avatar answered Sep 19 '22 01:09

Michael J. Calkins


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 } 
like image 33
Anshad Vattapoyil Avatar answered Sep 19 '22 01:09

Anshad Vattapoyil