JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

Using onclick to execute a PHP function

Author:JIYIK Last Updated:2025/04/13 Views:

We will also introduce another onclick()method of executing PHP functions through events using the jQuery library. This method calls a JavaScript function that will output the content of the PHP function in the web page.

We will also demonstrate another onclick()way to execute PHP functions using events, calling PHP functions using pure JavaScript.

This article will introduce a method to execute a PHP function, using GETthe method to send data in the URL and isset()the function to check GETthe data. This method calls a PHP function if the data is set and executes the function.


Use jQuery onclick()to execute PHP functions through events

We can use jQuery to execute an event by writing a function that executes a PHP function onclick(). For example, create a PHP file echo.phpand write a function php_func(). Write a message inside the function Have a great dayand call the function. In another PHP file, scriptwrite some jQuery inside a tag. Don't forget to link the web page with the jQuery source. In HTML, write a tag with onclick()an attribute button. Write the attribute value as test()a function. buttonWrite your text between the tags Click. Create an empty divtag below the button. scriptWrite the function inside the tag test(). echo.phpWrite an AJAX method with the URL of and resultwrite a function with as a parameter success(). Then use a selector to select divthe tag and use text()the function and resultas parameters.

In the following example, we use the AJAX method to perform an asynchronous HTTP request. URL specifies the URL to send the request to, and success()the function runs when the request is successful. The method sends the request to echo.phpthe file, which is located in the same location as the current PHP file. If the request is successful, success()the function returns the result, which is printed out.

Sample code:

#php 7.x
<?php
function php_func(){
    echo " Have a great day";
}
php_func();
?>
<script>
function test(){
    $.ajax({url:"echo.php", success:function(result){
    $("div").text(result);}
})
} 
</script>
<button onclick="test()"> Click </button>
<div> </div>

Output:

Have a great day

onclick()Execute PHP functions via events using pure JavaScript

This method uses JavaScript to execute onclick()a PHP function with an event. For example, write a PHP function php_func()that displays a message Stay Safe. buttonCreate a Clickbutton named using the tag. onclick()Assign the function as an attribute and clickMe()the function as its value. scriptWrite the function inside the tag clickMe(). Create a variable resultand call it inside the PHP tag php_func(). Use document.write()the function and resultas parameters to print the output.

In the example below, a JavaScript function clickMe()is executed when we click the button. Then, a PHP function is executed from the JavaScript function php_func(). resultA variable stores the result from the PHP function and is printed out.

Code example:

#php 7.x
<?php
function php_func(){
echo "Stay Safe";
}
?>
<button onclick="clickMe()"> Click </button>
function clickMe(){
var result ="<?php php_func(); ?>"
document.write(result);
}

Output:

Stay Safe

Execute PHP functions from links using GETthe method and functionisset()

We can GETset the URL of the link using data and check if the data has been isset()set using the function. We can create a PHP function and call the function if the data has been set. For example, write a function myFunction()and display a message inside the function Have a great day. Create a link using an anchor tag. hrefSet the attribute of the tag to index.php?name=true. Write a text between the anchor tags Execute PHP Function. Check if it is set using the function with $_GETthe variable . Call the function inside the block .isset()nameifmyFunction()

In the following example, GETdata is sent through the URL. nameThe value of is set to true. isset()The function returns true, the function myFunction()executes and displays a message.

Sample code:

# php 7.x
<!DOCTYPE HTML>
<html>
<?php
function myFunction() {
    echo 'Have a great day'.'<br>';
 }
if (isset($_GET['name'])) {
    myFunction();
}
?>
<a href='index.php?name=true'>Execute PHP Function</a>
</html>

Output:

Have a great day

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial