1

I am having difficulty with displaying HTML it seems. haha, let me explain.

I have 1 template file for "comments"... and it tells things where to go and such in the html. When Adding, Updating and Selecting any of the "comments"

IE:

<div class='comment'>
   <div>{$name}</div>
   <div>{$comment}</div>
</div>

So within my comment I need to pull the COMMENT from the database which includes, \n

So I go like this.

$comment = nl2br($comment);
<div class='comment'>
   <div>{$name}</div>
   <div>{$comment}</div>
</div>

And this does work... But when I do an UPDATE via jQuery I use,

$("#"+ target +"").replaceWith(responseText);

And the responseText includes all HTML... but some reason, it still is including the \n... and not

I don't know if this is a limitation with Javascript, or rendering issues. Just not sure where else to go here...Any thoughts?

Justin
  • 2,502
  • 7
  • 42
  • 77

2 Answers2

0

In the php file you are getting the comments with using jQuery try doing the following before echoing the data back

$comment=str_replace('\n\r', '<br />', $comment);
$comment=str_replace('\n', '<br />', $comment);
echo $comment;
LeeR
  • 1,609
  • 11
  • 29
  • Yeah that works. but when I receive the response back from Javascript is when it seems to not work. – Justin Jul 11 '11 at 22:12
0

Well this was a tad strange, there was some issues that I didn't fully test and sorry for maybe not clarifying. But mysql_real_escape_string() was causing issues with the \n being stored in the database.

There for I am looking at using this function instead. Found on php.net's website

function mysql_escape_mimic($value) {
        if(isset($value)) 
        {
            if(is_array($value)) {
                return array_map(__METHOD__, $value);
            }

            if(!empty($value) && is_string($value)) {
                //return str_replace( array('\\',   "\0",  "\n",  "\r",   "'",  '"',  "\x1a"), 
                //                  array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $value);

                return str_replace( array('\\',   "\0",  "\r",   "'",  '"',  "\x1a"), 
                                    array('\\\\', '\\0', '\\r', "\\'", '\\"', '\\Z'), $value);
            }

            return $value;
        }
    }
Justin
  • 2,502
  • 7
  • 42
  • 77