Click here to Skip to main content
11,584,288 members (51,505 online)
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 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 334
2 Dave Kreskowiak 240
3 Richard Deeming 220
4 virusstorm 194
0 OriginalGriff 1,663
1 Sergey Alexandrovich Kryukov 1,229
2 Abhinav S 701
3 Dave Kreskowiak 667
4 Suvendu Shekhar Giri 568


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2015
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