0

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
mudraya katusha
  • 171
  • 1
  • 8

2 Answers2

1

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"}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

"one" => include('success.php'), will only put the return value of the file into the "one" element of your array. If you don't return anything from it, it'll just be null.

If you want the output you will need to use output buffering:

ob_start();
require_once('success.php');
$var = ob_get_clean();

But I will suggest you to just send the names of the files you want to include, then you can load the content of those include's into a section with php, or send the html content using ajax

Hope it helps

stan chacon
  • 768
  • 1
  • 6
  • 19