deerly Posted July 20, 2009 Share Posted July 20, 2009 :-*Hi everyone! Hopefully I can explain this well enough so someone can help! 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> Link to comment https://forums.phpfreaks.com/topic/166565-solved-line-breaks-javascript-and-php-oh-my/ Share on other sites More sharing options...
deerly Posted July 20, 2009 Author Share Posted July 20, 2009 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! Link to comment https://forums.phpfreaks.com/topic/166565-solved-line-breaks-javascript-and-php-oh-my/#findComment-878402 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.