33

I've got a php script which generates HTML content. Is there a way to send back that HTML content through JSON to my webpage from the php script?

Hirvesh
  • 6,440
  • 14
  • 51
  • 70
  • 2
    If you're just sending markup back, why JSON encode at all? – dartacus Feb 21 '11 at 10:43
  • because a plugin and it has some limitations... – Hirvesh Feb 21 '11 at 10:54
  • There are some additional things you should consider beyond what `json_encode` provides, as explained in this post - [4 Things You Must Do When Putting HTML in JSON](http://www.thorntech.com/2012/07/4-things-you-must-do-when-putting-html-in-json/). – Jeff Jul 25 '12 at 19:30
  • You might want a conditional action based on the response i.e if operation successful then show HTML, if failed show appropriate error message – nmcilree Feb 15 '19 at 15:27

7 Answers7

31

Yes, you can use json_encode to take your HTML string and escape it as necessary.

Note that in JSON, the top level item must be an array or object (that's not true anymore), it cannot just be a string. So you'll want to create an object and make the HTML string a property of the object (probably the only one), so the resulting JSON looks something like:

{"html": "<p>I'm the markup</p>"}
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • @the_archer @T.J. Crowder : I am doing the same thing my array `{"success":"true","data":"'.nl2br($a).'","type":"text"}` where $a is text posted by user....after inserting user input into database I am sending this json array back to client – vikas devde Apr 01 '13 at 18:42
  • Whats the security concern sending HTML on JSON? – hiFI Mar 20 '18 at 06:40
3

Do Like this

1st put all your HTML content to array, then do json_encode

$html_content="<p>hello this is sample text";
$json_array=array(

'content'=>50,
'html_content'=>$html_content
);
echo json_encode($json_array);
Mahendra Jella
  • 4,631
  • 1
  • 29
  • 36
3

All string data must be UTF-8 encoded.

$out = array(
   'render' => utf8_encode($renderOutput), 
   'text' => utf8_encode($textOutput)
);

$out = json_encode($out);
die($out);
blumanski
  • 59
  • 1
  • 3
2

Just to expand on @T.J. Crowder's answer.

json_encode does well with simple html strings, in my experience however json_encode often becomes confused by, (or it becomes quite difficult to properly escape) longer complex nested html mixed with php. Two options to consider if you are in this position are: encoding/decoding the markup first with something like [base64_encode][1]/ decode (quite a bit of a performance hit), or (and perhaps preferably) be more selective in what you are passing via json, and generate the necessary markup on the client side instead.

Community
  • 1
  • 1
orionrush
  • 478
  • 4
  • 27
1

In PHP:

$data = "<html>....";
exit(json_encode($data));

Then you should use AJAX to retrieve the data and do what you want with it. I suggest using JQuery: http://api.jquery.com/jQuery.getJSON/

Mārtiņš Briedis
  • 16,205
  • 5
  • 49
  • 71
1

You can send it as a String, why not. But you are probably missusing JSON here a bit since as far as I understand the point is to send just the data needed and wrap them into HTML on the client.

Jan Zyka
  • 15,968
  • 15
  • 63
  • 105
1

All these answers didn't work for me.

But this one did:

json_encode($array, JSON_HEX_QUOT | JSON_HEX_TAG);

Thanks to this answer.

Daan
  • 2,437
  • 16
  • 37