1

Consider the following line of javascript with some php inserted inline.

 $("#suggestion").val("<?php echo $note; ?>");

Where $note is a value coming from a mysql database from a field of type text. If $note is plain text with no quotes and no line breaks then the javascript works and the text area with the id #suggestion is populated correctly.

Problem 1) If $note has line breaks the resulting javascript completely breaks and stops executing completely. The resulting javascript with line breaks appears like so: enter image description here

Problem 2) Also if $note has quotes and I use

 htmlentities($note, ENT_QUOTES) 

the resulting text area has &quot; instead of the actual character. Not using htmlentities and leaving the bare quote completely breaks the javascript too.

How can I get it so that the javascript won't break if there are newlines and so that the textarea won't show entities but rather the real characters?

Edit: This is a very specific issue, not a general "how do I pass variables back and forth" with javascript and php. I have the basics down but am experiencing a specific issue with line breaks stopping javascript that I have never encountered before so I don't believe this is a duplicate of the referenced question. Marking this as duplicate may as well be saying "Please learn everything there is to know about coding before asking question about coding." Or which answer on that page pertains to my line breaks from mysql text field breaking jquery val() and the rest of the javascript from executing.

Since I can't answer because it's marked duplicate I'll put the answer here. I had to do this in my php before outputting the variable:

 $note = str_replace("\r\n", "\\n", $note);
 $note = str_replace('"', '\"', $note);

Thank you to the kind commenters that pointed me in the correct direction.

S.Mason
  • 3,397
  • 2
  • 20
  • 33
  • @quentin why did you delete the answer I was going to try is. put it back! what is your problem dude? I'm just looking for help here. You marking this as duplicate when IT IS NOT a duplicate and then deleting a helpful response is just plain mean. – S.Mason Aug 19 '15 at 14:33
  • @Quentin Please inform me which answer on that page directly pertains to my question about line breaks and javascript. I looked through them and none addressed my question. – S.Mason Aug 19 '15 at 14:35
  • 1
    Not a duplicate. The linked answer in the duplicate tag does not mention dealing with new lines. – Andy Aug 19 '15 at 14:45
  • 1
    Use [str_replace](http://php.net/manual/en/function.str-replace.php). For issue #1, replace `\r\n` or `\n` with nothing ([see this related question](http://stackoverflow.com/questions/10825380/php-replace-double-lines-with-one-html-break)). For issue #2, replace the double quote (`"`) with backslash double quote (`\"`) ([see this related question](http://stackoverflow.com/questions/2424281/how-do-i-replace-double-quotes-with-single-quotes)) – Alvaro Montoro Aug 19 '15 at 14:45
  • 2
    The question is not a duplicate of the question it was marked as a duplicate of, but it has definitely been asked before (see my comment above) – Alvaro Montoro Aug 19 '15 at 14:47
  • @AlvaroMontoro thank you for pointing me in the correct direction, I posted the solution I used in the question because I'm unable to answer because it was marked duplicate. – S.Mason Aug 19 '15 at 15:19
  • Dealing with new lines in data is part of the general problem of passing data between PHP and JavaScript. The duplicate question doesn't mention new lines *specifically* but they are one of the causes of the problems with the code marked "this works sometimes, but sometimes it fails". All of the answers with a significant number of upvotes address escaping special characters, including new lines. Solving the general problem of passing data will solve the new line problem (it will just do it robustly instead of as the sort of hack you get if you deal with too narrow a problem). – Quentin Aug 19 '15 at 15:30
  • @S.Mason — I did not delete any answers. The only answers on the question were deleted by their owners, one because (in the words of the answerer "EDIT: deleting because the closing as duplicate is more useful") and the other because it was completely wrong. – Quentin Aug 19 '15 at 15:32
  • @AlvaroMontoro — Assuming that `#suggestion` is a textarea, removing the new lines entirely seems undesirable. – Quentin Aug 19 '15 at 15:35
  • @Quentin The "general problem" sure, but we're told to be very specific when asking questions. So I was as specific as I could be. You can see the solution I posted in the original answer is very specific to the issue I was having. Replacing the \r\n with \\n keeps the line breaks in text area. Are we not supposed to ask for specific questions when we have problems with these general approaches? Are we just supposed to know based on those general principals? I say no we should be asking specific focused questions like I did. – S.Mason Aug 19 '15 at 15:37
  • There's nothing wrong with asking a question about a problem that focuses on specific symptoms. It's just that the answers should deal with the problem and not only with the specific symptoms. – Quentin Aug 19 '15 at 15:39

0 Answers0