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:
var xmlhttp = new XMLHttpRequest();
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.
<?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:
npm install php
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.
<?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!