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
Release: 2024-12-10 21:56:14
Original
760 people have browsed it

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();
}
Copy after login
<!-- HTML Snippet -->
Hello there!
<a href='index.php?execute=true'>Execute PHP Function</a>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template