Home  >  Article  >  Backend Development  >  Example demonstrating how to call a PHP function when a button is clicked

Example demonstrating how to call a PHP function when a button is clicked

藏色散人
藏色散人Original
2021-08-25 09:42:4410408browse

The central content of this article is as mentioned in the title, which is to teach you how to call PHP functions when clicking a button. In fact, there are many ways to call PHP functions. In addition to performing this operation by clicking a button, you can also use Ajax, JavaScript and JQuery call PHP functions; but this article mainly introduces the button-oriented PHP function calling method.

Without further ado, here are two ways to implement how to call a PHP function when a button is clicked (using an HTML button to call a PHP function).

Method 1:

Note: Create an HTML form document containing HTML buttons. When the button is clicked, the POST method is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function is called.

The code is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>

<body style="text-align:center;">

<h1 style="color:red;">
    PHP中文网
</h1>

<h4>
    如何通过点击按钮调用PHP函数?
</h4>

<?php
if(array_key_exists(&#39;button1&#39;, $_POST)) {
    button1();
}
else if(array_key_exists(&#39;button2&#39;, $_POST)) {
    button2();
}
function button1() {
    echo "这是按钮1被选中";
}
function button2() {
    echo "这是被选中的按钮2";
}
?>

<form method="post">
    <input type="submit" name="button1"
           class="button" value="按钮1" />

    <input type="submit" name="button2"
           class="button" value="按钮2" />
</form>
</body>

</html>

The effect is as follows:

GIF 2021-8-25 星期三 上午 9-38-14.gif

  • ##array_key_exists() function: Check a certain Whether the specified key name exists in the array, returns true if the key name exists, and returns false if the key name does not exist.

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

Note: The following example is only based on the POST method:

The code is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body style="text-align:center;">
<h1 style="color:#17c4ff;">
    PHP中文网
</h1>
<h4>
    如何通过点击按钮调用PHP函数?
</h4>

<?php

if(isset($_POST[&#39;button1&#39;])) {
    echo "这是按钮1被选中";
}
if(isset($_POST[&#39;button2&#39;])) {
    echo "这是被选中的按钮2";
}
?>

<form method="post">
    <input type="submit" name="button1"
           value="按钮1"/>

    <input type="submit" name="button2"
           value="按钮2"/>
</form>
</body>

</html>

The effect is as follows:

GIF 2021-8-25 星期三 上午 9-39-28.gif

  • isset() function is used to detect whether the variable has been set and is not NULL; if unset() has been used to release a variable, it will return FALSE through isset(); if isset() is used Testing a variable that is set to NULL will return FALSE; it should also be noted that the null character ("\0") is not equivalent to the PHP NULL constant.

Finally, I would like to recommend the latest and most comprehensive "

PHP Video Tutorial"~ Come and learn!

The above is the detailed content of Example demonstrating how to call a PHP function when a button is clicked. 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