Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have created the following php script with javascript using a text editor and to run the php file I used XAMPP. But when I run the file I got an error. I saved the PHP file with the .php extension. At the below I am inserting the source code and the error message please try to solve my problem!
 
 
<?xml version="1.0" encoding="UTF-8"?>
Posted 22-Sep-12 23:17pm
Comments
Wes Aday at 23-Sep-12 5:21am
   
Count the number of quotes on that line....
Wes Aday at 23-Sep-12 5:21am
   
and then add a ;
Chiranthaka Sampath at 23-Sep-12 5:38am
   
I have change the source code as at the below but gives the same result
 
<pre>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP With JavasCript</title>
</head>
<body>
 
$a = 15;

if($a > 10){
echo <script language = "JavaScript" type="text/javascript">;

document.write("OK!");

</script>;
}


?>
</body>
</html>
 

</pre
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have to insert escape character (\) before each double quote that is inside a double-quoted string.
<?php
    $a = 15;
    if($a > 10){
        echo "<script type=\"text/javascript\">
                document.write(\"OK!\");
            </script>";
    }
?>
  Permalink  
Comments
Chiranthaka Sampath at 23-Sep-12 6:31am
   
Ok dude I owe you! The problem was solved many thanx!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Another way to solve it, without adding backslashes is "heredoc" syntax:
 
<?php
    $a = 15;
    if($a > 10){
        echo <<<JAVASCRIPT
<script type="text/javascript">
  document.write("OK!");
</script>
JAVASCRIPT;
    }
?></javascript>
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Huseyin Atasoy's escaping characters methods works well.
 
Or you could simply put single quotes(') around the echoing string like this.
 
echo '<script type="text/javascript">
          document.write("OK!");        
      </script>';
?>
 
That works too.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 458
1 OriginalGriff 344
2 George Jonsson 233
3 Animesh Datta 130
4 Shemeemsha RA 128
0 OriginalGriff 6,179
1 Sergey Alexandrovich Kryukov 5,616
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100