2012-03-28 69 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,那麼這將需要從服務器上執行。因爲它是一種服務器端語言。