Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML in a Javascript variable

Status
Not open for further replies.

myOtherHandle

Programmer
Jan 4, 2006
5
NO
Hi.

I have a variable: var html;

I need to put some HTML formatted text into that variable using PHP.

So: var html = '".somephpvariable."';

The problem is that the HTML from the php isn't on one line so I get an error saying that the string isn't terminated...

How can I get this to work?
 
That's the problem. I'm using a layered popup to let the user edit some text on a page.

If I remove these the text in the <textarea> box will look like a mess.
 
In that case, use PHP string replace to replace a line break with \n. This code takes advantages of the difference in PHP between single quotes and double quotes to do the replacement.
Code:
<?php
  [green]// where y is your multiline data[/green]
  z = str_replace("[blue]\n[/blue]", '[blue]\n[/blue]', y);

  echo('[blue]<script type="text/javascript">[/blue]');
  echo('[blue]  var foo = "[/blue]'.z.'[blue]";[/blue]');
  echo('[blue]</script>[/blue]');
?>

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
I ended up with:
$data = str_replace(chr(10), '\n', str_replace(chr(13), '\r', ($row['dbfield'])));

and it seems to work.

Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top