JavaScript Encode HTML for JSON string encode html json,html encode json string,html encode json

I am storing html in a mysql database as raw html. I am pulling the contents and placing it into an array as follows. The array is the json_encoded, but if there is any double quotes or urls then the javascript displaying the JSON string breaks.

Is it possible to encode html to work through JSON?

Here is an extract of what I am using

    <?php
    $rows = array();
    $sql ="SELECT html FROM table";

    try {  
        $sth = $dbh->query($sql); 
        while($r = $sth->fetch(PDO::FETCH_OBJ)) {
            $rows[] = $r;
        }
    }
    catch(PDOException $e) {  
        echo "I'm sorry, Dave. I'm afraid I can't do that. $e";  
    } 
    ?>

var json = JSON.parse('<?php print json_encode(json_encode($rows)); ?>');

The json string currently outputting throws a javascript error saying unexpected <

[{"html":"<a target='_blank' href="http:\/\/www.url.com">\n<img src="http:\/\/www.url.com\/img\/logo.png">\n<\/a>"}]

Please no lectures on why this may be a bad thing. This is what my client has requested specifically and I just need to know if it is possible.

Answer:1

There's no need for the json.parse business. JSON IS valid javascript after all, so a simple

var json = <?php echo json_encode($rows); ?>;

is all you need.

Answer:2

I agree with Mark B's answer but you could use fetchAll() and json_encode() the result of this function. Why do you use PDO Object fetching instead of array fetching?

<?php
$rows = array();
$sql ="SELECT hmtl FROM table";

try {  
    $sth = $dbh->query($sql); 
    $rows = $sth->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {  
    echo "I'm sorry, Dave. I'm afraid I can't do that. $e";  
} 
?>

var json = <?= json_encode($rows); ?>;

Moreover consider getting it via AJAX.

Answer:3

The line

var json = JSON.parse('<?php print json_encode(json_encode($rows)); ?>');

...is susceptible to single quotes in the resulting JSON (and other things; you'd have to double-escape lots of stuff). But you don't need or want to do that. Do this:

var json = <?php print json_encode(json_encode($rows)); ?>;

json_encode returns valid JSON, and JSON is a subset of JavaScript object initializer syntax, and so the above will result in valid JavaScript code describing the object. If you're outputting JavaScript code, as you appear to be from that line, there's no reason to go indirectly through a string. And as you've found, there's a reason not to.

Answer:4

I am trying to display options on an image on hover but when I hover the displayed options keeps flickering $('a').hover(function(event) { var href = $(this).attr('href'); var ...

I am trying to display options on an image on hover but when I hover the displayed options keeps flickering $('a').hover(function(event) { var href = $(this).attr('href'); var ...

I have been trying to toggle the background-color property of a button onclick but the color only changes once and does not toggle back and forth. Below is the code. function btnColor(btn, color) { ...

I have been trying to toggle the background-color property of a button onclick but the color only changes once and does not toggle back and forth. Below is the code. function btnColor(btn, color) { ...

  1. button background color css
  2. button background color android
  3. button background color react native
  4. button background color transparent
  5. button background color swift
  6. button background color tkinter
  7. button background color kivy
  8. button background color bootstrap
  9. button background color javafx
  10. button background color swiftui
  11. button background color opacity
  12. button background color material ui
  13. button background color android studio
  14. button background color none
  15. button background color not working
  16. button background color java
  17. button background color xcode
  18. button background color on hover
  19. button background color in html
  20. button background color javascript

I have looked for possible roots of my issue but have been unable to do so. I have some java Script that dynamically creates a list of check boxes. Some have text other have anchor links with text ...

I have looked for possible roots of my issue but have been unable to do so. I have some java Script that dynamically creates a list of check boxes. Some have text other have anchor links with text ...

  1. checkbox text not visible c#

I am simply calling as window.location = '../MyController/MyAction?campId=' + campId + '&campName=' + campName; and in action am receiving it as [HttpGet] public ActionResult MyAction(int ...

I am simply calling as window.location = '../MyController/MyAction?campId=' + campId + '&campName=' + campName; and in action am receiving it as [HttpGet] public ActionResult MyAction(int ...

  1. parameter value missing for '-top'
  2. parameter missing value ssrs
  3. invalid_parameter_value missing required field size
  4. xst - parameter value missing for '-top'
  5. parameter is missing value
  6. required parameter 'value' is missing for event 'purchase'
  7. missing parameter value of type interface org.gradle.logging.styledtextoutputfactory
  8. missing parameter value for aura.tag
  9. missing parameter value of type interface org.gradle.internal.factory
  10. missing parameter value in crystal report
  11. report viewer parameter missing value
  12. ssrs hidden parameter missing value
  13. a parameter value is missing from the subscription
  14. missing parameter value error in crystal report