Output HTML in JSON string generated by PHP
html, jquery, json, php
Solution
You need not escape the html with htmlentities on server side.
Remove the `$html = htmlentities($html);` from your php file.
Reason: htmlentities will convert
<strong>Some HTML</strong> <br /> Some more text.
to
<strong>Some HTML</strong> <br /> Some more text.
which when included in html will display:
<strong>Some HTML</strong> <br /> Some more text.
Problem
I have some PHP that outputs json. ``` <?php $html = utf8_encode($gegevens['tekst']); $html = htmlentities($html); //$html = htmlspecialchars($gegevens['tekst'], ENT_QUOTES, 'UTF-8'); echo json_encode(array( 'titel' => $gegevens['titel'], 'html' => $html )); ?> ``` The output will be like: ``` {"titel":"Here comes the title","html":"<strong>Here is the HTML<\/strong>\n<br \/>\n<br \/>\n And some more."} ``` And the jQuery/Ajax will be: ``` $.ajax({ type: "GET", url: "content/popup.php?id=" + id2, dataType: 'json', crossDomain: true, success: function(json) { var titel = json['titel']; var html = json['html']; function ContentTonen() { // Div's legen van content $('.popup_home_head_inside').empty(); $('.popup_home_content_inside').empty(); $('.popup_home_head_inside').html(titel); var html2 = html.replace(/\"/g, ""); //$('.popup_home_content_inside').html(html2); $('.popup_home_content_inside').html(html2); ``` And the HTML output is: ``` <strong>Some HTML</strong> <br /> Some more text. ``` So it will not process as HTML. Can you help me out?