Jez
Jez

Reputation: 260

Show results from database in jquery

How can I use the content in $displayusercontent which is pulled from the db into the Text field on the jquery.

I've tried this below, but nothing returns, if I echo $displayusercontent on to the page then it works this way.

<html>
<head>
</head>
<body>
   <!-- my content here-->

   <script type="text/javascript">
      var userContent = <?php echo $displayusercontent; ?>
   </script>       
   <script type="text/javascript" src="/my/javascript/file/here.js"></script>
</body>
</html>

This is from my jquery file

initIntro: function () {
// display marketing alert only once
if (!$.cookie('intro_show')) {
    setTimeout(function () {
        var unique_id = $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: 'MyTitle',
            // (string | mandatory) the text inside the notification
            text: userContent,
            // (string | optional) the image to display on the left
            //image: '../../assets/local/layout/img/avatar.png',
            // (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'
        });

        // You can have it return a unique id, this can be used to manually remove it later using
        setTimeout(function () {
            $.gritter.remove(unique_id, {
                fade: true,
                speed: 'slow'
            });
        }, 15000);
    }, 2000);

    $.cookie('intro_show', 1);
}
}

Upvotes: 0

Views: 70

Answers (2)

Spokey
Spokey

Reputation: 10994

It's invalid. You need to make it a string

var userContent = <?php echo json_encode($displayusercontent); ?>;

As @dsclementsen pointed out, in this case using json_encode is the right option.

Upvotes: 1

Jez
Jez

Reputation: 260

After the posts and reading the links attached, this is my code that works.

<html>
<head>
</head>
<body>
    <!-- my content here-->

   <script type="text/javascript">
       var userContent = <?php echo json_encode($displayusercontent) ?>;
   </script>       
   <script type="text/javascript" src="/my/javascript/file/here.js"></script>
</body>
</html>

Thanks all for the help :-)

Upvotes: 1

Related Questions