-1

My code is written below:

<?php
      $message = "<script type="text/javascript">
        $(document).ready(function(){
            demo.initChartist();
            $.notify({
                icon: 'pe-7s-gift',
                message: "hello world."
            },{
                type: 'info',
                timer: 4000
            });
        });
    </script>";
?>

inside html

<?php echo $message; ?>
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
  • Did you try after solved quotes issue , replace inside `"` double quotes to `'` single quotes and try – Niklesh Raut Dec 30 '17 at 12:12
  • dont put double quotes inside double use altenate quotes e.g. single under double or double under single quotes . – Sachin Dec 30 '17 at 12:14

3 Answers3

1

Best way to do this without having to worry about quotes is using HEREDOC.

You can do this:

$message = <<<JS
        $(document).ready(function(){
              demo.initChartist();
                $.notify({
                    icon: 'pe-7s-gift',
                    message: 'hello world.'
                },{
                    type: 'info',
                    timer: 4000
                });
            });
JS;

print '<script>'.$message.'</script>';

Or if you need to put the script tags inside the variable; you can use EOT instead of JS.

Phright
  • 179
  • 4
0

Just replace internal " double quotes into single quotes '

<?php
 $message = "<script type='text/javascript'>
    $(document).ready(function(){
      demo.initChartist();
        $.notify({
            icon: 'pe-7s-gift',
            message: 'hello world.'
        },{
            type: 'info',
            timer: 4000
        });
    });
 </script>";
?>
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
0

If you are trying to echo the <script> element onto the webpage this should work.

The only thing that will be in your way is the usage of quotation marks. Right now you are trying to use double quotes inside a string made up with double quotes. This will only work if you escape the quotes inside the string (which can be done using the escape character \ like so "\"").

Of course it is also possible to replace the double quotes by single quotes.

Take a look at this question for a broader example/explanation.

yarwest
  • 873
  • 8
  • 19