从JSON执行PHP函数 [英] Executing PHP function from JSON

查看:95
本文介绍了从JSON执行PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JSON和PHP进行某些操作,尽管我不确定100%地确定有一种方法,但有些方法我找不到.但是因为它看起来是一个不错的选择(如果可能的话),所以我决定在这里询问.

I am trying some things with JSON and PHP and there is something that I can't find a way to do, though I'm not 100% sure there is one. But because it looks like a nice option (If possible) I decided to ask here.

我有来自jquery官方站点的这些示例.有两个文件,第一个是index.php,我在其中执行我的Ajax,但它是:

I have these examples from jquery offical site. There are two files, the first one is index.php where I execute my Ajax, hete it is:

<!DOCTYPE html>
<html>
<head>
<title>Simple form sending and receiving a JSON object to/from PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  var data = 
  { 
    "sales": [ 
      { "firstname" : "John", "lastname" : "Brown" },
      { "firstname" : "Marc", "lastname" : "Johnson" }
    ] // end of sales array
  }
  var dataString = JSON.stringify(data);
  $.post('simpleformSubmi.php', { data: dataString}, showResult, "text");
});

function showResult(res)
{
  $("#fullresponse").html("Full response: " +res);
}
</script>
<div id="fullresponse"></div>
</head>
<body>

没有什么复杂的.我有我的simpleformSubmi.php:

Nothing complicated at all. And I have my simpleformSubmi.php which is :

<?php

$logFile = 'logFile';
$res = json_decode(stripslashes($_POST['data']), true);
error_log("result: ".$_POST['data'].", res=".json_encode($res), 3, $logFile);
error_log("\n", 3, $logFile);

//header("Content-type: text/plain");
foreach ($res as $key=>$value)
{
    $str[] = $value;
}
$functionArray ="function(){ \$a = 10; echo \$a;}";
$jsStr = $str[0][1];
echo json_encode($jsStr['firstname']);
echo '<hr />';
echo json_encode($res);
echo '<hr />';
echo json_encode($functionArray);
?>

您可以看到$ functionArray-实际上是一个包含PHP函数的字符串,我想使用JSON返回该字符串并在此之后执行它.那么有没有办法做到这一点呢?现在我在执行文件的index.php中得到的是:

As you can see $functionArray - is in fact a string containing PHP function which I want to return back using JSON and to execute it after that. So is there any way to do that really? Now what I get in index.php afet executing the files is:

"function(){ $a = 10; echo $a;}"

谢谢 莱恩

推荐答案

似乎您正在尝试通过JavaScript执行PHP函数.由于PHP是在服务器端执行的,因此在这种情况下执行PHP函数的唯一方法是,例如通过执行另一个ajax调用,要求服务器返回为您执行该函数.

Seems like you're trying to execute a PHP function through JavaScript. Since PHP is executed server-side the only way you have to execute a PHP function in that context is to ask the server back to execute the function for you, by doing another ajax call for example.

类似这样的东西:

index.php

index.php

$(document).ready(function(){ 
  var data = 
  { 
    "sales": [ 
      { "firstname" : "John", "lastname" : "Brown" },
      { "firstname" : "Marc", "lastname" : "Johnson" }
    ] // end of sales array
  }
  var dataString = JSON.stringify(data);

  //Change post() success callback function to executePHP()
  $.post('simpleformSubmi.php', { data: dataString}, executePHP, "text");

  //Let's define executePHP() outside post() call for clarity
  function executePHP()
  {
      //Ask the server to execute function foo(), and get the result
      $.get("example.com/somefile.php?function=foo", function(data)
      {
          //Success function, do whatever you want.
          alert(data);
      });
  }
});

然后,在somefile.php中

Then, in somefile.php

<?php
//Condition(s), if any. You could even implement this interface using REST.
//Get function to execute
if($_GET["function"] == "foo")
{
    //Output function's result.
    echo foo();
}

//The function you want to run
function foo()
{
    //Do something
    $a = 10;
    return $a;
}
?>

如果一切顺利,当JavaScript到达alert(data);语句时,您将看到10.

If all went well, when JavaScript reaches the alert(data); statement you will see 10.

这篇关于从JSON执行PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆