我正在尝试一些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