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:
Problem 2) Also if $note has quotes and I use
htmlentities($note, ENT_QUOTES)
the resulting text area has "
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.