Forum Moderators: coopster

Message Too Old, No Replies

passing javascript text variable to php

         

jackvull

12:36 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Hi
I have a vriable in my script such as:
$message = "Hi";

I then try to pass this to some javascript but because of the quotes, something is getting messed up (I get an unterminated string constant error). Taking out the quotes from the document.write section doesn't work...

ANy ideas on how to fix this?

?>
<script language = 'javascript' type='text/javascript'>
NewWin = window.open('', 'newwin', 'height=250, width=250,toolbar=no,scrollbars='+scroll+',menubar=no');
NewWin.document.write("<TITLE>Title Goes Here</TITLE>");
NewWin.document.write("<BODY>");
NewWin.document.write("<?php echo $message;?>");
NewWin.document.write("</BODY>");
NewWin.document.write("</HTML>");
</script>
<?

Thanks.

dcrombie

12:38 pm on Oct 6, 2005 (gmt 0)



Have you tried:

<?php echo addslashes($message); ?>

;)

jackvull

12:53 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Doesn't work.
I've played around a bit and it seems that if I have the variable as:
$message = "<html>
<img src =''>";
then it doesn't like it.

However if I put the variable text in without line returns:
$message = "<html><img src =''>";

it prints fine.

Problem is that the formatting is then very difficult to read...

dcrombie

1:20 pm on Oct 6, 2005 (gmt 0)



The you need something like this (not sure on exact number of \'s):

<?php echo preg_replace("/\r?\n/", "\\n", addslashes($message)); ?>

jackvull

1:43 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Did the job perfectly.
Thanks!