Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

How to Call PHP Function on Button Click?

Posted on July 22, 2025July 22, 2025 By Admin No Comments on How to Call PHP Function on Button Click?

In PHP web development, PHP code is executed on the server-side before the page is sent to the user’s browser. This means you cannot call a PHP function directly when a button is clicked in the browser—unlike JavaScript. However, you can trigger a PHP function as a result of a user action (like clicking a button) by submitting a form (page reload/post) or making an AJAX request.

Below are the most common and practical methods to run PHP functions on button click:

1. Using an HTML Form (Page Reload Method)

Here, clicking the button submits the form to the same or another PHP script, which can then call the PHP function.

Example:

<?php
function showAlert() {
    echo "<script>alert('Button clicked! PHP function executed.');</script>";
}

if(isset($_POST['myButton'])) {
    showAlert();
}
?>
<form method="post">
    <button type="submit" name="myButton">Click Me</button>
</form>

How it works?

  • The button submits the form.
  • PHP checks if the button was clicked using isset($_POST['myButton']).
  • The PHP function runs on server-side and outputs a JavaScript alert.

2. Using AJAX (No Page Reload, More Dynamic)

With AJAX (using JavaScript/jQuery), you can send a request to PHP when a button is clicked, and PHP can respond. This does not reload the page.

HTML + JavaScript:

<button id="callPhp">Call PHP Function</button>
<div id="result"></div>

<script>
document.getElementById("callPhp").onclick = function(){
    fetch("ajax-handler.php", { method: "POST" })
      .then(response => response.text())
      .then(data => document.getElementById("result").innerHTML = data);
}
</script>

ajax-handler.php

<?php
function getMessage() {
    return "This message is from a PHP function!";
}

echo getMessage();
?>

3. Using URL Query Strings (GET Method)

For simple get requests, clicking a link/button can append a parameter to the URL and PHP can check and run a function:

<?php
function greet() {
    echo "Hello from PHP function!";
}

if(isset($_GET['greet'])) {
    greet();
}
?>
<a href="?greet=1"><button>Greet</button></a>

Summary Table

MethodPage ReloadMore DynamicUsage
Form POST/GETYesNoSimple actions/forms
AJAXNoYesNo reload, modern SPA
URL Query (GET)YesNoSimple url-based calls

Important Note

  • PHP code runs on the server. You can only “call a PHP function on button click” by re-submitting data to the server (with a form or AJAX).
  • For client-side interactivity (without reloads), use AJAX to call a server-side PHP script.
PHP, Web Technologies Tags:PHP-Questions

Post navigation

Previous Post: How to pop an alert message box using PHP?
Next Post: Java Collections Framework

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Selenium
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme