Hey all,

I have a field in my table called 'message'. Now when i write to this field i do:

"UPDATE tablename SET message='".nl2br($msgcontent)."'"

This then replaces any newlines with '<br />' .

Now... when i grab this value back and try to put it into a textarea i get a javascript error:

'Unterminated string constant'

I just can't figure out how to solve this. I've tried to do a:

str_replace("<br />", "\n", $msgcontent);

But that didnt work.

Anyone got any suggestions please?? Many Thanks,

BIOSTALL

    Hard to say with so little information/code, but I know for a fact that javascript can't handle a lot of newlines in a string somehow.

    So you could tokenize your string on the newlines and concanate them in javascript again.

      This is my PHP getting the value from the DB:

      $members = $row['members'];

      Because i use 'nl2br' when i write it to the db, it contains some '<br />' occurences.

      If i then try and put this value into a textarea using javascript:

      document.formeditband.members.value = "<?php echo $members ?>";

      i get the error: 'Unterminated string constant'.

      Hope thats explained it a bit better for y'all.

      Thanks,

      BIOSTALL

        You'll need to replace <br /> with \n. Notice the double backslash, because you want Javascript to see "\n", not "
        ".

        You'll also need to replace " with \".

          Thanks weedpacket... but the problem remains 🙁

          I'm now using:

          $members = $row['members'];
          $members = str_replace("<br />", "\\n", $members);

          I'm still getting the same error.

          Any further ideas please??

          T'is much appreciated.

          BIOSTALL

            If \n didn't work, then your problem may be something else.

            When javascript gives an error, it always says the line number where that error occured. Take a look at the html source and see what is going on at that line number.

              Of course, if you're using IE to test the javascript, the line number will probably be very wrong. But yes; debug the Javascript first and then you'll know what to do to the PHP.

                Write a Reply...