I am not familiar with the Eldarion AJAX plugin. I've just read the README, and to be honest I don't like it. (Though it does win points for having a cool name.)
There's too much "magic" that goes on. You don't get to specify the format in which you send and receive data. If you want to change things, you have to do so with HTML data-
attributes. To me, violates the principle of separation of concerns, i.e. that each part of the structure of the site should have one purpose. For instance, the HTML should define the structure of the page, the CSS the styling, and the Javascript the interactivity. To have HTML attributes defining AJAX activity seems absurd to me.
But no matter. Let's look at the specific question you have, which is how to return the data to Eldarion. Let's look at the README file again:
Easily extend support on the server side code for this by adding a top-level attribute to the JSON you are already returning called "html" that is the rendered content. Unlike a backbone.js approach to building a web app, eldarion-ajax leverages server side template rendering engines to render and return HTML fragments.
So Eldarion expects to receive a JSON structure, with a top-level property called html
that contains the rendered content. So it expects a structure approximately like this:
{
"html": "<p>The content that should be appended to #result.</p>"
}
So let's do that with your content. Let's not mess about trying to make the JSON ourselves. It's too difficult to do right and far too easy to make mistakes. Fortunately, PHP comes to our aid with the json_encode
function, which encodes an array as a valid JSON structure for you.
$text = $_POST['text'];
$data = array(
'html' => $text
);
echo json_encode($data); // output the data as JSON
I've set up a jsFiddle example that shows how this works. Obviously it's a bit different, because I had to use the jsFiddle API, but you can see approximately how it works.