Click here to Skip to main content
12,240,822 members (59,087 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Javascript PHP web-dev
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"?>
<!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>
    <?php
 
        $a = 15;

        
        if($a > 10){
            echo "<script type="text/javascript">
            
                    document.write("OK!");            
            
                </script>";
        }
    
    
    ?>
Posted 23-Sep-12 0:17am
Comments
Wes Aday 23-Sep-12 5:21am
   
Count the number of quotes on that line....
Wes Aday 23-Sep-12 5:21am
   
and then add a ;
Chiranthaka Sampath 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 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;
    }
?>
  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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2016
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