Home >Backend Development >PHP Tutorial >How Can I Execute a PHP Function with an HTML Anchor Click Without Page Refresh?

How Can I Execute a PHP Function with an HTML Anchor Click Without Page Refresh?

Barbara Streisand
Barbara StreisandOriginal
2024-12-10 21:56:14791browse

How Can I Execute a PHP Function with an HTML Anchor Click Without Page Refresh?

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:

  • PHP scripts execute on the server and respond to requests.

HTML & JavaScript:

  • HTML and JavaScript run in the browser.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn