Calling PHP Functions on Click
Issue:
Execute a PHP function upon clicking an HTML anchor tag (without refreshing the page).
Solution:
To execute a PHP function on a click event, a multi-language approach is required, involving three languages:
PHP:
HTML & JavaScript:
Mechanism:
Assuming a PHP file contains both code:
// Function function runMyFunction() { echo 'PHP function executed!'; } // Handle request if (isset($_GET['execute'])) { runMyFunction(); }
<!-- HTML Snippet --> Hello there! <a href='index.php?execute=true'>Execute PHP Function</a>
When the link is clicked, it sends a GET request to the index.php page with the 'execute' parameter. The script then handles the request and executes the runMyFunction() PHP function.
The above is the detailed content of How Can I Execute a PHP Function with an HTML Anchor Click Without Page Refresh?. For more information, please follow other related articles on the PHP Chinese website!