I want to pass a string variable from PHP to javascript, I tried the third method in this answer, but I when I got the value (textarea.value) from the textarea, I lost the leading newline characters.
I tried to add a character like “=” at the beginning and the end of my string, it seems that I solved this problem. Are there any ways better to deal with this problem? Are there any other problems which can cause I getting different value that I may encounter?
PHP code:
<textarea id="data"><?php echo htmlspecialchars("nna"); ?></textarea>
javascript code:
let a=document.getElementById("data").value;
// expect: "nna"
// actual: "na"