-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; ?>
2
  • Did you try after solved quotes issue , replace inside " double quotes to ' single quotes and try Commented Dec 30, 2017 at 12:12
  • dont put double quotes inside double use altenate quotes e.g. single under double or double under single quotes . Commented Dec 30, 2017 at 12:14

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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>";
?>

Comments

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.

1 Comment

I'd like someone to explain the downvote?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.