SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2008
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with PHP spaces

    Hi,

    I am getting the problem when i used the php code inside javascript. i,e

    Code:
    <?
    $description=<p><strong>This is Test1</strong></p><p><strong>This is Test2</strong></p>
    ?>
    <script>
    alert('<?=$description?>');
    </script>
    Above code works fine and give the alert message when page will loaded.

    i have modified that above code like that

    Code:
    <?
    $description=<p><strong>This is Test1</strong></p>
    <p><strong>This is Test2</strong></p>
    ?>
    <script>
    alert('<?=$description?>');
    </script>
    It didn't worked and didn't give any alert message. Anybody Could please tell the solution to my problem.




    Thanks
    Swetha

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php
    $description='<p><strong>This is Test1</strong></p>\n<p><strong>This is Test2</strong></p>';
    ?>
    <script>
    alert("<?php echo $description; ?>");
    </script>
    Note the types of quote marks used, and the \n for a new line
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use quotes:
    PHP Code:
    <?
    $description
    ="<p><strong>This is Test1</strong></p><p><strong>This is Test2</strong></p>";
    ?>
    <script>
    alert('<?=$description?>');
    </script>
    my mobile portal
    ghiris.ro

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2008
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,



    I am getting the 'description' from database text datatype .For to get an problem only i have directly declared like that . How can i add the \n in this case. Could please give an clear idea





    Thanks
    Swetha

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2008
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi


    I already using the quotes .By mistake at the time of posting i forgot it . It is not my problem .When i use the paragraph tags in same line it works but when i use the paragraph tags separately in each line i am getting the problem.

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the <p> shouldn't cause any problems: it's just text to be displayed "as is" by the alert box.

    To get the new line character correct:
    PHP Code:
    $description str_replace("\n",'\n',$description); 
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you want to pass a string to javascript, json_encode() is very useful. It will quote, and properly escape the contents of the string into something suitable for javascript.
    PHP Code:
    <?
    $description 
    '
    <p><strong>This is Test1</strong></p>
    <p><strong>This is Test2</strong></p>
    '
    ;
    ?>
    <script>
    alert(<?=json_encode($description)?>);
    </script>


Bookmarks

Posting Permissions

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