Connecting Tech Pros Worldwide Forums | Help | Site Map

php var with embedded LF to javascript var

Newbie
 
Join Date: Feb 2007
Posts: 4
#1: Feb 8 '07
Hi,
Is there a way to place php var with LF such as: $p="string1 \n string2"
into: var j="<? echo $p ?>";

I am getting an error in j assigment: Unterminated string constant
Thanks


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?
$p="string1 \n string2"
?>
<script>
var j="<? echo $p ?>";
alert(j);
</script>
</body>
</html>

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Feb 8 '07

re: php var with embedded LF to javascript var


Welcome th The Scripts!

It will work when you code the php variable assignment within single quotes, as follows:
Expand|Select|Wrap|Line Numbers
  1. $p='string1\nstring2';
Ronald :cool:
Newbie
 
Join Date: Feb 2007
Posts: 4
#3: Feb 8 '07

re: php var with embedded LF to javascript var


Quote:

Originally Posted by ronverdonk

Welcome th The Scripts!

It will work when you code the php variable assignment within single quotes, as follows:

Expand|Select|Wrap|Line Numbers
  1. $p='string1\nstring2';
Ronald :cool:

====================================
Thanks
It worked !
Jose
Reply