user8568304
user8568304

Reputation:

Run JavaScript in PHP then Uncaught SyntaxError occur

When I run it without PHP then this running well.

      <?php 

       $it="$getuser[active]"; if($it==1)
       { echo '<script type="text/javascript">';
       echo ' $(document).ready(function () {
       var unique_id = $.gritter.add({
        // (string | mandatory) the heading of the notification
        title: "Welcome to my website!",
        // (string | mandatory) the text inside the notification
        text: "Activate Your Account Now!!<a href="active.php" target="_blank" style="color:#ffd777">Click Here</a>.",
        // (string | optional) the image to display on the left
        image: "img.jpg",
        // (bool | optional) if you want it to fade out on its own or just 
        sit there
        sticky: true,
        // (int | optional) the time you want it to be alive for before 
        fading out
        time: "",
        // (string | optional) the class name you want to apply to that specific message
        class_name: "my-sticky-class"
    });';

       echo '  return false;
    });';
    echo '</script> ';
   }
   ?>

Upvotes: 0

Views: 35

Answers (1)

B. Desai
B. Desai

Reputation: 16436

You have to escape ". text: "Activate Your Account Now!!<a href="active.php" target="_blank" style="color:#ffd777">Click Here</a>.", this line will create issue also remove line break before "sit there" and "fading out"

<?php 
$it="$getuser[active]"; if($it==1)
{ 
     echo '<script type="text/javascript">';
     echo ' $(document).ready(function () {
     var unique_id = $.gritter.add({
      // (string | mandatory) the heading of the notification
      title: "Welcome to my website!",
      // (string | mandatory) the text inside the notification
      text: "Activate Your Account Now!!<a href=\"active.php\" target=\"_blank\" style=\"color:#ffd777\">Click Here</a>.",
      // (string | optional) the image to display on the left
      image: "img.jpg",
      // (bool | optional) if you want it to fade out on its own or just  sit there
      sticky: true,
      // (int | optional) the time you want it to be alive for before fading out
      time: "",
      // (string | optional) the class name you want to apply to that specific message
      class_name: "my-sticky-class"
  });';

     echo '  return false;
  });';
  echo '</script> ';
 }
 ?>

EDIT

You can skip php part also and just write script directly

<?php 
$it="$getuser[active]"; if($it==1)
{ ?>
    <script type="text/javascript">
     $(document).ready(function () {
     var unique_id = $.gritter.add({
      // (string | mandatory) the heading of the notification
      title: "Welcome to my website!",
      // (string | mandatory) the text inside the notification
      text: 'Activate Your Account Now!!<a href="active.php" target="_blank" style="color:#ffd777">Click Here</a>.',
      // (string | optional) the image to display on the left
      image: "img.jpg",
      // (bool | optional) if you want it to fade out on its own or just  sit there
      sticky: true,
      // (int | optional) the time you want it to be alive for before fading out
      time: "",
      // (string | optional) the class name you want to apply to that specific message
      class_name: "my-sticky-class"
  });

     return false;
  });
  </script>
 <?php }
 ?>

Upvotes: 1

Related Questions