|
-
Oct 23rd, 2005, 05:00 AM
#1
Thread Starter
Addicted Member
PHP/Javascript new line problem
Hi all,
This is my PHP getting the value from the DB:
PHP Code:
$row=mysql_fetch_array($result);
$members = $row['members'];
Because i use 'nl2br' when I write it to the db, it contains some '<br />' occurences.
If i then try and put this value into a textarea using javascript:
document.formeditband.members.value = "<?php echo $members ?>";
I get the error: 'Unterminated string constant'.
Anyone got any suggestions to why i might be getting this error and how to covercome it please?
Many Thanks,
BIOSTALL
-
Oct 24th, 2005, 02:53 PM
#2
Lively Member
Re: PHP/Javascript new line problem
What's the output when you echo $members?
-
Oct 24th, 2005, 05:34 PM
#3
Re: PHP/Javascript new line problem
You need to ensure that the text has all quotations escaped; that is ' and ", if they are not, JS will end the string constant prematurely. The PHP function [url=http://www.php.net/addslashes]addslashes()[/php] will do this. However, you also need to escape newline characters with their equivalents too, so I would recommend you use str_replace()
PHP Code:
$js_safe_members = str_replace(array('"',"'", "\r", "\n", "\0"), array('\"','\\\'','\r', '\n', '\0'), $members);
?>
... other html
document.formeditband.members.value = "<?php echo($js_safe_members) ?>";
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|