Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
Hy, so my problem is that i have a textarea which is generated with jquery, like this:
$var input = $('<textarea name="somename"></textarea>');
$(something).after(input);
But i also want to fill this textarea with content (when content exists of course), using php. sp now my code looks like this:
$var input = $('<textarea name="somename"><?=$variabel[0][something];?</textarea>');
$(something).after(input);
So the problem with this is that when i run this code the javascript automatically fills the content of the textarea in that input variable, and because there is more than one line it breaks so i get an error saying “Unexpected token ILLEGAL”
I hope i explained it well, so i need somehow the put the content of the textarea inside that generated textarea, but in a way in which my input doesn’t break.
Thans for any advice!
You can use the php function json_encode() to encode your PHP variable before it is output. It will convert newlines to \n, for example. More details about the function can be found here
http://php.net/manual/en/function.json-encode.php
In your case it would be something like this:
Also refer to the following question: Pass a PHP string to a JavaScript variable (and escape newlines)