How to call PHP function on the click of a Button?

How to call PHP function on the click of a Button? We will likewise acquaint one more strategy with execute a PHP work with the onclick() occasion utilizing the jQuery library. This technique calls a JavaScript work which will yield the substance of the PHP work in the site page.

We will likewise exhibit one more strategy to execute a PHP work with the onclick() occasion utilizing plain JavaScript to call the PHP work.

This article will acquaint a strategy with execute a PHP work utilizing the GET technique to send the information in the URL and the isset() capacity to really look at the GET information. This strategy calls the PHP work assuming that the information is set and executes the capacity.

How to Call PHP function with the click of a Button?

We can utilize jQuery to execute the onclick() occasion by composing a capacity that will execute the PHP work. For instance, make a PHP record echo.php and compose a capacity php_func(). Compose a message Have an extraordinary day inside the capacity and call the capacity. In another PHP record, think of some jQuery inside the content tag. Remember to interface the website page with the jQuery source. In the HTML, compose a button tag with onclick() property. Compose the worth of the property as the test() work. Compose the text Click between the button labels. Make a void div tag underneath the button. Compose the capacity test() inside the content tag. Compose an AJAX technique with the URL as echo.php and compose a triumph() work with result as the boundary. Then, at that point, utilize the selector to choose the div tag and utilize the text() work with result as the boundary.

In the model beneath, we utilize the AJAX technique to play out a nonconcurrent HTTP demand. The URL species the URL to send the solicitation to, and the achievement() work runs when the solicitation is effective. The technique sends the solicitation to the echo.php document, which dwells in a similar area as the current PHP record. The solicitation becomes fruitful, and the achievement() work returns the outcome, and it gets printed.

Given an archive containing HTML and PHP code and the assignment is to call the PHP work subsequent to tapping on the button. There are different strategies to take care of this issue. Likewise, aside from doing this with the snap of a button, a PHP capacity can be called utilizing Ajax, JavaScript, and JQuery. However, this article for the most part centers around the button arranged methodology of calling the PHP Function.

Calling a PHP work utilizing the HTML button: Create a HTML structure report which contains the HTML button. Whenever the button is tapped the technique POST is called. The POST strategy depicts how to send information to the server. In the wake of tapping the button, the array_key_exists() work called.

Program 1:

<!DOCTYPE html>
<html>
     
<head>
    <title>
        How to call PHP function
        on the click of a Button ?
    </title>
</head>
 
<body style="text-align:center;">
     
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
     
    <h4>
        How to call PHP function
        on the click of a Button ?
    </h4>
     
    <?php
        if(array_key_exists('button1', $_POST)) {
            button1();
        }
        else if(array_key_exists('button2', $_POST)) {
            button2();
        }
        function button1() {
            echo "This is Button1 that is selected";
        }
        function button2() {
            echo "This is Button2 that is selected";
        }
    ?>
 
    <form method="post">
        <input type="submit" name="button1"
                class="button" value="Button1" />
         
        <input type="submit" name="button2"
                class="button" value="Button2" />
    </form>
</head>
 
</html>

Output:

eevibes

Program 2: This program uses isset() function to call PHP function.

<!DOCTYPE html>
<html>
     
<head>
    <title>
        How to call PHP function
        on the click of a Button ?
    </title>
</head>
 
<body style="text-align:center;">
     
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
     
    <h4>
        How to call PHP function
        on the click of a Button ?
    </h4>
 
    <?php
     
        if(isset($_POST['button1'])) {
            echo "This is Button1 that is selected";
        }
        if(isset($_POST['button2'])) {
            echo "This is Button2 that is selected";
        }
    ?>
     
    <form method="post">
        <input type="submit" name="button1"
                value="Button1"/>
         
        <input type="submit" name="button2"
                value="Button2"/>
    </form>
</head>
 
</html>

Output:

eevibes

Also ReadIterate over a List in Python: What is a List in Python?

Leave a Reply

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