Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle text with `ln` using PDO
    text
    copied!<p>I am using PDO to execute queries. I have this problem: I have a <code>&lt;textarea&gt;</code> element from witch I am getting the text and post it to the server side and add it to the db after I am preparing it with PDO prepare:</p> <pre><code>$insertQuery = $db-&gt;prepare("INSERT INTO feedback (userName, comment, time_stamp, languageCode, userIp) VALUES (:usernaem, :comment, '". $now ."', :lang, '". $ip ."')"); $insertQuery-&gt;bindValue(':comment',nl2br($_POST['comment'])); ... </code></pre> <p>In some other place in my web I am getting this "comment" back from the db and fetch it to json string:</p> <pre><code> try { $commentQuery = $db-&gt;query("SELECT userName, time_stamp, comment, languageCode FROM feedback LIMIT 10"); } catch (PDOException $ex) { die(json_encode(array('outcome' =&gt; false, 'message' =&gt; $ex-&gt;getMessage()))); } $result = $commentQuery-&gt;fetchAll(PDO::FETCH_ASSOC);} $commentsJson = json_encode($result); </code></pre> <p>the <code>$commentsJson</code> I am inserting to some javascript var:</p> <pre><code>var comments = JSON.parse('&lt;?php echo $commentsJson; ?&gt;'); </code></pre> <p>and here my chrome browser throws an exception.</p> <h2>For example:</h2> <p>if I have this text in my <code>&lt;textarea&gt;</code>: and I submit it to the db: </p> <blockquote> <p>This is some comment</p> <p>and this is new line of comment </p> </blockquote> <p>in the db it saved as:</p> <pre><code>This is some comment&lt;br /&gt; and this is new line of comment </code></pre> <p>But when I am getting it back from the db I am getting this:</p> <pre><code> var comments = JSON.parse('[{"userName":"Test","time_stamp":"2013-04-04 10:17:43","comment":"This is some comment&lt;br \/&gt;\nand this is new line of comment", ... </code></pre> <p>and after that I have my browser exception saying: </p> <blockquote> <p>"Uncaught SyntaxError: Unexpected token"</p> </blockquote> <p>Any help would be appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload