Home  >  Article  >  Backend Development  >  How to call php method in js

How to call php method in js

WBOY
WBOYOriginal
2023-05-22 20:54:072602browse

JavaScript and PHP are two different programming languages. The former runs in the browser, while the latter runs on the server side. Although the two languages ​​run in different environments, they can interact in specific ways. In this article, we will introduce how to call PHP methods in JavaScript.

1. Use AJAX to implement JavaScript calls to PHP methods

AJAX (Asynchronous JavaScript And XML) is a technology that transmits through JavaScript and XML. It can be used without refreshing the entire page. , communicate asynchronously with the server. This technique can be used to call PHP methods in JavaScript.

The implementation is as follows:

  1. First, you need to create an XMLHttpRequest object
var xmlhttp = new XMLHttpRequest();
  1. Then, you need to create a JavaScript function to use Data is sent to the PHP server
function sendDataToPHP(data) { 
   xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
         document.getElementById("result").innerHTML = this.responseText;
      }
   };
   xmlhttp.open("POST", "test.php", true);
   xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlhttp.send(data);
}

This function receives a parameter data, which is the data to be sent to the server. Inside the function, a callback function is first created to process data when the server returns it. When the value of the readyState attribute changes to 4, it means that the server response is completed. When the HTTP status code is 200, it means OK. Then, communicate with the PHP server through the open() method of the XMLHttpRequest object, and finally, use the send() method to send the data to the server.

  1. The PHP server needs to receive the data sent by JavaScript first, then execute the corresponding function and return the result.
<?php
    function myFunction($param1, $param2) {
        //TODO
        return $result;
    }
    
    $request = file_get_contents('php://input');
    $data = json_decode($request);
    $result = call_user_func_array($data->functionName, $data->params);
    echo $result;
?>

In this code snippet, a function myFunction that needs to be called in JavaScript is defined, and the call_user_func_array method is used to pass data to the function and execute it. Finally, the function return value is output to the browser.

2. Use Node.js to call PHP methods in JavaScript

Node.js is a JavaScript running environment based on the Chrome V8 engine. It is a very powerful server-side framework that can be used For calling PHP methods in JavaScript.

The implementation is as follows:

  1. First, you need to install the PHP plug-in in the project
npm install php
  1. Create a JavaScript file for calling PHP Function
var php = require('php');
var result = php.myFunction('param1', 'param2');
console.log(result);

In this code snippet, the require method is used to introduce the php module into the JavaScript environment, and the php.myFunction method is used to call the PHP function. Finally, print the function return value to the console.

  1. The PHP server needs to export functions that need to be called in JavaScript, which can be achieved by using the $GLOBAL object.
<?php
    $GLOBAL['myFunction'] = function($param1, $param2) {
        //TODO
        return $result;
    };
?>

In this code snippet, the $GLOBAL object allows PHP functions to be used as global functions. Therefore, PHP functions can be called in JavaScript through the function name.

Summary:

The above are two ways to call PHP methods in JavaScript. AJAX and PHP modules are very convenient. Using the above techniques you can easily call PHP functions in JavaScript and get the return value.

The above is the detailed content of How to call php method in js. 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