从JSON执行PHP函数

时间:2012-03-28 09:03:59

标签: php jquery ajax json

我正在尝试使用JSON和PHP的一些东西,有些东西我找不到办法,虽然我不是100%确定有一个。但因为它看起来是一个不错的选择(如果可能的话)我决定在这里问。

我在jquery官方网站上有这些例子。有两个文件,第一个是index.php,我执行我的Ajax,它是:

<!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,它是:

<?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中得到的文件是:

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

由于 Lern

5 个答案:

答案 0 :(得分:2)

好像你正试图通过JavaScript执行PHP函数。由于PHP是在服务器端执行的,因此在该上下文中执行PHP函数的唯一方法是通过执行另一个ajax调用来请求服务器为您执行该函数。

这样的事情:

的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中

<?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

答案 1 :(得分:1)

在将响应作为响应发送后,您无法执行PHP函数,因为客户端收到响应,而PHP是服务器端语言。

通常,您只需返回值。在您的示例中,您只需返回一个包含键值对a,10的关联数组。

您可以从PHP脚本返回javascript函数,并使用eval在客户端执行该函数,但eval'ing会打开潘多拉盒子的安全漏洞。

答案 2 :(得分:0)

您无法在PHP服务器之外执行PHP代码。所以你不能在浏览器中运行它。

但是,您可以传递一串JavaScript并通过eval运行它。有些人会告诉你这很糟糕,但请记住eval曾经是解析JSON的唯一方法。

答案 3 :(得分:0)

为了向PHP发回内容,您必须通过表单中的GET或POST操作通过p.e调用服务器端。但是,不,你不能通过echo执行任何服务器端,因为echo输出到客户端。您总是可以在服务器端使用eval(http://php.net/manual/es/function.eval.php)来执行POST消息中的某些操作,但不建议这样做,因为它可以打开一个很大的安全漏洞。

答案 4 :(得分:0)

你已经返回了一个函数(我假设你的意思是javascript),现在你需要调用它。这可以通过使用jQuery $ .post成功回调来完成。

尝试更改此内容..

$.post('simpleformSubmi.php', { data: dataString}, showResult, "text");

$.post('simpleformSubmi.php', { data: dataString}, function(data){eval(data)}, "text");

如果它的PHP(它看起来像)而不是Javascript,那么这将需要从服务器执行。因为它是一种服务器端语言。