我正在嘗試一些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
你不能得到JSON來執行任何PHP ... – 2012-03-28 09:06:32