Results 1 to 3 of 3

Thread: PHP/Javascript new line problem

  1. #1

    Thread Starter
    Addicted Member BIOSTALL's Avatar
    Join Date
    Apr 2005
    Location
    Northampton, UK
    Posts
    180

    PHP/Javascript new line problem

    Hi all,

    This is my PHP getting the value from the DB:
    PHP Code:
    $row=mysql_fetch_array($result);
    $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'.

    Anyone got any suggestions to why i might be getting this error and how to covercome it please?

    Many Thanks,

    BIOSTALL

  2. #2
    Lively Member {yak}'s Avatar
    Join Date
    Aug 2005
    Posts
    119

    Re: PHP/Javascript new line problem

    What's the output when you echo $members?
    {yak}

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP/Javascript new line problem

    You need to ensure that the text has all quotations escaped; that is ' and ", if they are not, JS will end the string constant prematurely. The PHP function [url=http://www.php.net/addslashes]addslashes()[/php] will do this. However, you also need to escape newline characters with their equivalents too, so I would recommend you use str_replace()
    PHP Code:
    $js_safe_members = str_replace(array('"',"'", "\r", "\n", "\0"), array('\"','\\\'','\r', '\n', '\0'), $members);
    ?>
    ... other html
    document.formeditband.members.value = "<?php echo($js_safe_members?>";
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width