Jump to content

[SOLVED] Line breaks, javascript and PHP oh my!


deerly

Recommended Posts

:-*Hi everyone!

 

Hopefully I can explain this well enough so someone can help!  :confused:

 

Using jQuery I am updating a mysql table with some POST data.

 

Then I want to send the POST data into a javascript function so that it does fancy things to appear on the page.

 

This all works great UNTIL I put in line breaks!

 

The \n in PHP was causing the javascript to give me an "unterminated string literal" error so I did the following:

 

$string_for_js = str_replace("\n", "\\n", $safe_value);

echo "<script language=javascript>hideMe('$safe_id', '$string_for_js')</script>";

 

So here is the problem! When the text is displayed on the page, there are no line breaks and, since the page is not reloading, I am not sure how to format it so that it does display with line breaks.

 

Refreshing the page, str_replace does the trick (n2br makes my w3 strict settings freak out) but what can I do before that?

 

Also, the hide me function does:

function hideMe(myDiv, myText){

myDiv = document.getElementById(myDiv);

$(myDiv).hide();

$(myDiv).text(myText);

}

 

 

Update

Thought I'd include this before anyone responded -- I tried javascript replace() method but that just prints a <p>

8)

 

Figured it out! I guess I just was looking for some fresh eyes but stepping away was good enough!

 

For anyone who is curious... I did a str_replace to make the "\n" into </p><p> before sending the string through JS (instead of escaping the "\n")

 

Then... I had to change the jQuery from .text(blahblah) to .html(blahblah) DUH!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.