SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: Problem with PHP spaces
-
Jul 20, 2009, 04:17 #1
- 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>
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>
Thanks
Swetha
-
Jul 20, 2009, 04:46 #2
- 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>
-
Jul 20, 2009, 04:49 #3
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
-
Jul 20, 2009, 04:57 #4
- 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
-
Jul 20, 2009, 05:03 #5
- 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.
-
Jul 20, 2009, 05:29 #6
- 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);
-
Jul 20, 2009, 07:18 #7
- 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