2010-05-25 46 views
0

在我的数据库中,我打算创建一个存储消息的表来提醒用户需要做的任何事情。在PHP和MySQL中使用jQuery Growl

我正在寻找使用jQuery咆哮像通知方法,但我很困惑我将如何开始构建它。

数据将从表单中使用标准的MySQL插入方法添加到数据库中,但是如何从数据库中选择消息以使用jQuery咆哮显示。

这是否需要使用AJAX?

这是JavaScript代码,我到目前为止,我不知道我将如何实现PHP代码在它旁边,这样我可以从我的桌子拉出来的数据显示为通知:

 <script type="text/javascript"> 

    // In case you don't have firebug... 
    if (!window.console || !console.firebug) { 
     var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"]; 
     window.console = {}; 
     for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() {}; 
    } 

    (function($){ 

     $(document).ready(function(){ 

      // This specifies how many messages can be pooled out at any given time. 
      // If there are more notifications raised then the pool, the others are 
      // placed into queue and rendered after the other have disapeared. 
      $.jGrowl.defaults.pool = 5; 

      var i = 1; 
      var y = 1; 

      setInterval(function() { 
       if (i < 3) { 
        $.jGrowl("Message " + i, { 
         sticky:   true, 
         log:   function() { 
          console.log("Creating message " + i + "..."); 
         }, 
         beforeOpen:  function() { 
          console.log("Rendering message " + y + "..."); 
          y++; 
         } 
        }); 
       } 

       i++; 
      } , 1000); 

     }); 
    })(jQuery); 

    </script>      
       <p> 

</span> 
<p> 

回答

1

PHP是在服务器上运行并且JavaScript正在客户端上运行。

所以是的,你需要AJAX。

那么,会有其他方法,但他们比简单地设置AJAX更多的工作。特别是,因为你使用jQuery来处理大部分AJAX的东西。

让它调用一个小的PHP脚本,从DB获取行,以您喜欢的方式输出它们(XML或JSON)并退出。

通常的jQuery AJAX教程应该完全覆盖。

如果您的应用程序是多用户,请不要忘记在请求中发送用户标识,以便PHP知道要拉取哪些行。