如何在php中将include绑定到关联数组
我有两个来自php脚本的echo,它们作为json发送到ajax调用。这2个回波将以2个不同的div输出。 对于这两个echo,我创建了如下数组:如何在php中将include绑定到关联数组,php,json,ajax,include,associative-array,Php,Json,Ajax,Include,Associative Array,我有两个来自php脚本的echo,它们作为json发送到ajax调用。这2个回波将以2个不同的div输出。 对于这两个echo,我创建了如下数组: $result = [ "one" => "this is echo 1", "two" => "this is echo 2" ]; echo json_encode($result); 我现在想包括2个文件(将是echos),而不是这些echo文件。我能做到吗 所以我想要的是这样的: $result = [
$result = [
"one" => "this is echo 1",
"two" => "this is echo 2"
];
echo json_encode($result);
我现在想包括2个文件(将是echos),而不是这些echo文件。我能做到吗
所以我想要的是这样的:
$result = [
"one" => include('success.php'),
"two" => include('renderfiles.php')
];
我该怎么做
顺便说一下,这是我的jquery ajax:
$.ajax({
url: "",
type: "post",
data: new FormData(this),
dataType: 'json',
contentType: 'application/json',
success: function(data) {
$('.echo').html(data.one); // content ofinclude success.php should come here
$('.table-content').html(data.two); // content of include renderfiles.php should come here
在include文件中,您需要
返回
HTML-或者使用捕获它,然后返回内容。使用返回
$result = [
"one" => include('success.php'),
"two" => include('renderfiles.php')
];
因此success.php的内容类似于
return "<sometag></sometag>";
{"one":"<sometag><\/sometag>","two":...}
<sometag></sometag>{"one":1,"two":"a"}
你可能会得到类似的结果
return "<sometag></sometag>";
{"one":"<sometag><\/sometag>","two":...}
<sometag></sometag>{"one":1,"two":"a"}
{“一”:1,“二”:“一”}
“one”=>include('success.php')只会将文件的返回值放入数组的“one”元素中。如果你没有从中返回任何东西,它将是空的
如果需要输出,则需要使用输出缓冲:
ob_start();
require_once('success.php');
$var = ob_get_clean();
但是我建议您只发送想要包含的文件的名称,然后您可以使用php将这些包含的内容加载到一个节中,或者使用ajax发送html内容
希望对您有所帮助您希望success.php包含什么?在这两个包含中,都是带有htmlyou的php代码,您将收到包含的结果(0或1),而不是内容。。。如果需要PHP未解析,请使用file_get_contents,使用解析的html,使用outputbueffing截取直接输出。