Home > Backend Development > PHP Tutorial > How Can I Execute PHP Functions with Button Clicks Using AJAX?

How Can I Execute PHP Functions with Button Clicks Using AJAX?

Mary-Kate Olsen
Release: 2024-12-04 21:00:16
Original
856 people have browsed it

How Can I Execute PHP Functions with Button Clicks Using AJAX?

Executing PHP Functions on Button Click

You have created a PHP page with two buttons intended to call specific functions, but you're not getting the expected output. The issue lies in the approach you're using.

To correctly execute a PHP function when a button is clicked, you need to utilize Ajax for asynchronous communication between the page and the server. Here's a modified version of your code that incorporates Ajax:

Modified Markup:

<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />
Copy after login

jQuery:

$(document).ready(function() {
    $('.button').click(function() {
        var clickBtnValue = $(this).val();
        var ajaxurl = 'ajax.php',
        data = {'action': clickBtnValue};
        $.post(ajaxurl, data, function(response) {
            // Response div goes here.
            alert("action performed successfully");
        });
    });
});
Copy after login

ajax.php:

<?php
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'insert':
                insert();
                break;
            case 'select':
                select();
                break;
        }
    }

    function select() {
        echo "The select function is called.";
        exit;
    }

    function insert() {
        echo "The insert function is called.";
        exit;
    }
?>
Copy after login

Explanation:

  • The click() event is used to capture button clicks.
  • When a button is clicked, the corresponding function name is passed as the action value in the Ajax request.
  • The ajax.php script, which contains the function definitions, is called asynchronously using $.post().
  • After the server-side script executes the function, the response is received and an alert message is displayed.
  • The exit statement prevents the server-side script from executing further code after the function call, ensuring that the response is not sent multiple times.

With this approach, when you click on either the "Insert" or "Select" button, the corresponding function will be executed, and you will see an alert message indicating the successful execution.

The above is the detailed content of How Can I Execute PHP Functions with Button Clicks Using AJAX?. 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