Home Backend Development PHP Tutorial 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

Aug 25, 2021 am 09:42 AM
php function button

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>
Copy after login

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>
Copy after login
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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Change the power button action on Windows 11 [5 Tips] Change the power button action on Windows 11 [5 Tips] Sep 29, 2023 pm 11:29 PM

The power button can do more than shut down your PC, although this is the default action for desktop users. If you want to change the power button action in Windows 11, it's easier than you think! Keep in mind that the physical power button is different from the button in the Start menu, and the changes below won't affect the operation of the latter. Additionally, you'll find slightly different power options depending on whether it's a desktop or laptop. Why should you change the power button action in Windows 11? If you put your computer to sleep more often than you shut it down, changing the way your hardware power button (that is, the physical power button on your PC) behaves will do the trick. The same idea applies to sleep mode or simply turning off the display. Change Windows 11

Why won't my laptop start up after pressing the power button? Why won't my laptop start up after pressing the power button? Mar 10, 2024 am 09:31 AM

There could be several reasons why your Windows laptop won't boot. Memory failure, dead battery, faulty power button, or hardware issues are all common causes. Here are some solutions to help you resolve this issue. Laptop won't turn on after pressing the power button If your Windows laptop still won't turn on after pressing the power button, here are some steps you can take to resolve the issue: Is your laptop fully charged? Perform a hard reset to clean your laptop Reseat the memory Transparent CMOS type battery Take your laptop for repair. 1] Is your laptop fully charged? The first thing to do is to check if your laptop is fully charged. Laptop won't start if battery is drained

What does iPhone 16 look like? What changes are there in iPhone 16? What does iPhone 16 look like? What changes are there in iPhone 16? Apr 07, 2024 pm 05:10 PM

After the release of the iPhone 15 series, there have been constant revelations about the appearance and configuration of Apple’s new iPhone 16. What does iPhone 16 look like? Is there any improvement in iPhone 16? Recently, an overseas blogger showed off the design of the iPhone 16 series. The overall design is basically the same as the iPhone 15 series. As you can see from the picture, the entire iPhone 16 series is equipped with a new "shoot" button as standard, allowing users to take photos more conveniently. In addition, other design details are still unknown. The message shows that this new button will be used to shoot videos and is located below the power button. Previous news has mentioned that it may be a capacitive solid-state button, but recent reports indicate that it should still be a

How to optimize the lazy loading effect of images through php functions? How to optimize the lazy loading effect of images through php functions? Oct 05, 2023 pm 12:13 PM

How to optimize the lazy loading effect of images through PHP functions? With the development of the Internet, the number of images in web pages is increasing, which puts pressure on page loading speed. In order to improve user experience and reduce loading time, we can use image lazy loading technology. Lazy loading of images can delay the loading of images. Images are only loaded when the user scrolls to the visible area, which can reduce the loading time of the page and improve the user experience. When writing PHP web pages, we can optimize the lazy loading effect of images by writing some functions. Details below

How to solve the problem of unresponsive buttons in IE browser How to solve the problem of unresponsive buttons in IE browser Jan 30, 2024 am 10:48 AM

What should I do if there is no response when clicking the web button in IE browser? If there is no response when we click the web button, we can set it in the compatibility view! When some friends are using the IE browser, they find that the browser will not respond when clicking the button on the web page. In this way, we cannot use the functions of the web page. How can we set it up? The editor has compiled the IE browser below. How to solve the problem of the browser not responding when clicking the web button? If not, follow me and read on! IE browser does not respond when clicking the web button. Solution: 1. Open IE browser, click the [Tools] button on the operation bar, and click [Compatibility View] settings, as shown in the figure. 2. In the [Compatibility View] setting page, click the [Add] button on the right and fill in the website.

iPhone 16 and 16 Plus introducing Action button iPhone 16 and 16 Plus introducing Action button Oct 03, 2023 pm 12:13 PM

In the iPhone16 series, the Pro series screen will become larger. As for the iPhone16 standard version and iPhone16Plus, they will remain almost the same as the iPhone15 and 15Plus currently on the market, with only a slight weight increase of 2g. This also means that Apple does not plan to re-launch iPhone mini, but will continue to launch iPhone Plus. Adding an Action Button iPhone 16 will also have some new features, such as the Action button of the iPhone 15 Pro series, or even a new “Capture button”. Changed to iPhone12 camera vertical arrangement iPhone16 standard version and iPhone

HTML, CSS and jQuery: Make a button with a floating effect HTML, CSS and jQuery: Make a button with a floating effect Oct 24, 2023 pm 12:09 PM

HTML, CSS and jQuery: Making a button with a floating effect requires specific code examples. Introduction: Nowadays, web design has become an art form. By using technologies such as HTML, CSS and JavaScript, we are able to add various aspects to the page. Such special effects and interactive effects. This article will briefly introduce how to use HTML, CSS and jQuery to create a button with a floating effect, and provide specific code examples. 1. HTML structure First, we need to

How to disable the action button on iPhone 15 Pro and 15 Pro Max How to disable the action button on iPhone 15 Pro and 15 Pro Max Nov 07, 2023 am 11:17 AM

Apple brought some Pro-exclusive hardware features to iPhone 15 Pro and 15 Pro Max, which attracted everyone’s attention. We're talking titanium frames, sleek designs, the new A17 Pro chipset, an exciting 5x telephoto lens, and more. Of all the bells and whistles added to the iPhone 15 Pro models, the action button remains a prominent and prominent feature. Needless to say, it is a useful addition to launching actions on your iPhone. That said, you could accidentally hold down the Action button and trigger the feature inadvertently. Frankly, it's annoying. To avoid this, you should disable the action button on iPhone 15 Pro and 15 Pro Max. let

See all articles