I have a textarea in a separate frame from the frame my PHP is in. As far as I know, if I want to dynamically set the values of the textarea upon loading the php page, I have to use both PHP and JavaScript. Here is what I have now:

echo '<script type="text/javascript"><!--
parent.otherFrame.document.formName.fieldName.value="', $comment, '";
// -->
</script>';

The problem is that when $comment contains any kind of line breaks, the JavaScript won't execute. I have tried nl2br(), but '<br />' will just show up in the textarea, instead of actual line breaks. I read that in order to have JavaScript put line breaks in a field, you need to use '\r\n' where the breaks would be

$comment = 'This is the first line\r\nThis is the second'

But I can't get PHP to output any new line characters as '\r', '\n', or '\r\n'. I have tried almost every string function available, but It's not working. :mad:

Any thoughts?

    You have to slash them ->

    echo "\n" produces \n

      I've tried that:

      $comment = 'line1
      
      line3
      
      
      line6';
      
      echo addslashes($comment);

      yeilds this: (for some reason)
      [font=courier]line1

      line3


      line6[/font]

      not this:
      [font=courier]line1\n\nline3\n\n\nline6[/font]

        6 days later

        I figured it out. I had tried to use str_replace() to replace "<br />" with "\n", but the problem was that I was enclosing the new line character in double quotes ("\n") instead to single quotes ('\n'). In a doubly quoted string, PHP will interpret all escape sequences, however, in a singly quoted string, PHP will only pay attention to 2 escape sequences - one for the single quote character \' and one for the escape character \. In this case, I didn't want PHP to interpret the new line, I wanted it to be printed literally for JavaScript to interpret.

          Write a Reply...