douhuan1380 2019-04-12 17:16
浏览 78
已采纳

如何将include绑定到php [duplicate]中的关联数组

This question already has an answer here:

I have 2 echo's from a php script that are sent as json to the ajax call. These 2 echos will be outputted in 2 different divs. For those 2 echo's , I created an array like below:

 $result = [
    "one" => "this is echo 1",
    "two" => "this is echo 2"
];
echo json_encode($result);

Instead of these echo's, I now want to include 2 files (which will be the echos). Sow can I do that?

So what I want is something like this:

$result = [
    "one" => include('success.php'),
    "two" => include('renderfiles.php')
];

How can I do this?

By the way, this is my 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
</div>
  • 写回答

2条回答 默认 最新

  • doulingna9420 2019-04-12 17:26
    关注

    In your include files, you will need to return the HTML - or use output buffering to capture it and then return the contents. Using return ...

    $result = [
        "one" => include('success.php'),
        "two" => include('renderfiles.php')
    ];
    

    So the contents of success.php would be something like

    return "<sometag></sometag>";
    

    This makes sure that the value is passed back and inserted in the correct place and will give something like

    {"one":"<sometag><\/sometag>","two":...}
    

    If you just echo the HTML,

    echo "<sometag></sometag>";
    

    you may end up with something like

    <sometag></sometag>{"one":1,"two":"a"}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。