简体   繁体   English

从JavaScript按名称调用PHP函数

[英]Call PHP function by name from JavaScript

I have a javascript function that's executed on a button click and I have an included file containing all my PHP functions. 我有一个通过单击按钮执行的javascript函数,并且我包含一个包含所有PHP函数的文件。 One of the PHP functions returns me an array of filenames encoded as a JSON object. PHP函数之一返回给我一个编码为JSON对象的文件名数组。 I'd like to call this function from my Javascript function so that I can use the array. 我想从我的Javascript函数中调用此函数,以便可以使用数组。 I know I can call a PHP file which would run but in this case I only want to call one specific named method. 我知道我可以调用一个可以运行的PHP文件,但是在这种情况下,我只想调用一个特定的命名方法。

I tried the following but couldn't get it to work (Console error: Unexpected < ) 我尝试了以下操作,但无法正常工作(控制台错误:意外<)

function getPHPArray(){
    var picArray;
    picArray = <?php getPicArrayJson(); ?>;
}

Can this be done using XMLHttpRequest or do I need to find another method? 可以使用XMLHttpRequest完成此操作,还是需要找到其他方法? (If reasonably possible I'd prefer straight Javascript over JQuery but I can use JQuery if it makes this significantly easier) (如果有合理的可能,我更喜欢直接使用Javascript而不是JQuery,但可以使用JQuery来简化此工作)

If so how? 如果可以,怎么办?

My PHP method is as follows: 我的PHP方法如下:

function getPicArrayJson(){
    global $fileArrayString;
    If (!empty($fileArrayString)){
        echo json_encode($fileArrayString);
    }
}

My JavaScript so far: 到目前为止,我的JavaScript:

function getPHPArray(){
    var xmlhttp;
if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}   
xmlhttp.open("POST", "phpFunctions.php?name=getPicArrayJson", true);   
xmlhttp.send();
}

This is essentially a Remote Procedure Call . 这本质上是一个远程过程调用 For use with PHP, I'd check out JSON-RPC PHP . 要与PHP配合使用,我将签出JSON-RPC PHP


Anyway, here's a basic way you can do it without much complexity to your app. 无论如何,这是一种基本的实现方法,而无需增加应用程序的复杂性。 You will have to make an ajax request to the server. 您将必须向服务器发出ajax请求。 The idea for you here is to use call_user_func . 这里的想法是使用call_user_func

call_user_func("getPicArrayJson");

You can use 您可以使用

call_user_func($_POST["name"]);

But I highly recommend that you only allow certain functions to be called 但我强烈建议您仅允许调用某些函数


phpFunctions.php phpFunctions.php

$allowed_functions = array("getPicArrayJson");
$name              = $_POST["name"];

function getPicArrayJson() {
  $pics = array("1.jpg", "2.jpg");
  return json_encode($pics);
}

// legal function
if (in_array($name, $allowed_functions)) {
  header("Content-Type: application/json");
  $json = call_user_func($name);
  die($json);
}

// invalid access
else {
  // Error 400
}

Then do you Ajax request per normal 那你按正常要求Ajax请求吗

...
xmlhttp.open("POST", "phpFunctions.php?name=getPicArrayJson", true);   
xmlhttp.send();

ps my own two cents, I would use a GET request for this. ps我自己的两分钱,我会为此使用GET请求。 Following RESTful API conventions for web services , a POST request would only be used to create a new element. 遵循针对Web服务的RESTful API约定POST请求将仅用于创建新元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM