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 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.
hmtl, isn't ithtml?