2012-03-28 99 views
2

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

我从jquery官方网站有这些例子。有两个文件,第一个是index.php文件,我执行我的阿贾克斯,烯酸,它是:

<!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 - 其实,我想回回来使用JSON,之后执行它含有字符串PHP函数。那么有什么办法可以做到吗?现在我得到的index.php afet执行文件是:

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

感谢 Lern

+1

你不能得到JSON来执行任何PHP ... – 2012-03-28 09:06:32

回答

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

你不能因为在接收到响应发送它的响应后执行PHP函数客户端和PHP是一个服务器端语言。

通常情况下,你只需返回值。在您的例子,你只需返回保存键值对a,10关联数组。

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

+0

是的,阅读你的评论让我觉得现在有点愚蠢。 OBV。没有办法在客户端执行PHP脚本,但关于JS函数和eval的第二部分对我来说似乎很有趣。 – Leron 2012-03-28 09:12:36

+0

仔细阅读有关'eval'的危害以及如何安全使用它。 – xbonez 2012-03-28 09:14:00

0

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

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

0

为了向PHP返回某些东西,必须通过p.e从表单中通过GET或POST操作调用服务器端。但是,不,你不能通过echo执行任何服务器端的事情,因为echo输出到客户端。您可以在服务器端始终使用eval(http://php.net/manual/es/function.eval.php)从POST消息中执行某些操作,但不建议这样做,因为它可能会导致严重的安全漏洞。

0

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

尝试修改此..

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

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

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