【问题标题】:how to use results from database in jquery如何在jquery中使用数据库中的结果
【发布时间】:2014-03-06 21:08:21
【问题描述】:

如何从 '.$user['Content'].' 加载内容在文本字段中? 如果有任何解决方案,请帮助我

谢谢你

           $('#add-max').ready(function(){

            $.gritter.add({

           // (string | mandatory) the heading of the notification

            title: 'This is a notice with a max of 3 on screen at one time!',
            // (string | mandatory) the text inside the notification

            text: 'HERE ',
            // (string | optional) the image to display on the left
            image: 'http://a0.twimg.com/profile_images/59268975/jquery_avatar_bigger.png',
            // (bool | optional) if you want it to fade out on its own or just sit there
            sticky: false,
            // (function) before the gritter notice is opened
            before_open: function(){
                if($('.gritter-item-wrapper').length == 3)
                {
                    // Returning false prevents a new gritter from opening
                    return false;
                }
            }
        });

        return false;

    });

【问题讨论】:

  • 您的 ajax 代码在哪里?和相关的php代码?如果您没有这些,则无法使用 jquery 将数据获取到您的页面。
  • @VolkanUlukut 我没有 ajax 代码,如果我在其他地方使用 $user['Content'] 而不是 javascript 代码,它可以工作。
  • 请编辑您的问题并将其与其他相关的 php 代码一起添加
  • @VolkanUlukut 我的问题有什么问题??离开它...感谢您的帮助

标签: javascript php jquery sql


【解决方案1】:

你会在你的 PHP 视图/输出文件中做这样的事情:

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

       <!-- assign js vars from PHP here -->
       <script type="text/javascript">
          var userContent = <?php echo $user['Content']; ?>
       </script>

       <!-- load your other custom JS files after. Best practice is to put these at the bottom of <body> -->
       <script type="text/javascript" src="/your/javascript/file/here.js"></script>
   </body>
</html>

基本上,您在任意 javascript 标记块中声明一个 javascript 变量,并将您的 PHP 数据回显到该变量中。

然后所有后续脚本都可以访问该变量,您可以随意使用它。

那么要使用您的 jQuery 代码,您可以将之前定义的 userContent 变量作为 text 字段的值。

       $('#add-max').ready(function(){

        $.gritter.add({

       // (string | mandatory) the heading of the notification

        title: 'This is a notice with a max of 3 on screen at one time!',
        // (string | mandatory) the text inside the notification

        text: userContent,
        // (string | optional) the image to display on the left
        image: 'http://a0.twimg.com/profile_images/59268975/jquery_avatar_bigger.png',
        // (bool | optional) if you want it to fade out on its own or just sit there
        sticky: false,
        // (function) before the gritter notice is opened
        before_open: function(){
            if($('.gritter-item-wrapper').length == 3)
            {
                // Returning false prevents a new gritter from opening
                return false;
            }
        }
    });

    return false;

});

【讨论】:

  • 您好,感谢您的回答,但这没有用,我按照您说的做了,但没有加载,在页面源 var userContent = 看起来不错,我从 db 收到了我想要的东西,但是当我在文本字段中使用 userContent 时,它没有显示任何内容。
  • 我做到了,我将 $user['Content'] 放入我隐藏它的 div 中,然后我声明 var userContent = $( "div.content" ).text() 现在它工作:) 感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多