niche Posted November 14, 2011 Share Posted November 14, 2011 How do I get the line break to work in an alert box based on data from this php variable? <?php$myPhpVar = '"Hello again! This is how we" . "\n" . "add line breaks to an alert box!"';echo '<script type="text/javascript">';echo 'alert(' . $myPhpVar . ');';echo '</script>';?> Link to comment Share on other sites More sharing options...
JamesB Posted November 14, 2011 Share Posted November 14, 2011 for multi line strings in js you can do: var str = 'abcde\fghij' so the real \n gets escaped with a \ this should work: echo 'alert(' . str_replace("\n", "\\\n", $myPhpVar) . ');'; Link to comment Share on other sites More sharing options...
Ingolme Posted November 14, 2011 Share Posted November 14, 2011 Instead of "\n", use single-quotes so that it's not parsed: '\n' The alternative is to escape the backslash: "\\n" Link to comment Share on other sites More sharing options...
dsonesuk Posted November 14, 2011 Share Posted November 14, 2011 Just use it as would in javascript outputted text $myPhpVar = '"Hello again! This is how we\n add line breaks to an alert box!"'; Link to comment Share on other sites More sharing options...
niche Posted November 15, 2011 Author Share Posted November 15, 2011 That's what I needed. Thanks JamesB,Ingolme, and dsonesuk. Niche Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.