在我的数据库中,我计划创建一个表来存储消息,以提醒用户需要执行的任何操作。
我正在考虑使用类似 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>
最佳答案
PHP 在服务器上运行,JavaScript 在客户端上运行。
所以,是的,您需要 AJAX。
嗯,还有其他方法,但它们比简单地设置 AJAX 需要更多工作。特别是因为您使用 jQuery,它会为您处理大部分 AJAX 内容。
让它调用一个小型 PHP 脚本,从数据库中获取行,以您喜欢的方式(XML 或 JSON)输出它们并退出。
通常的 jQuery AJAX 教程应该涵盖这一点。
如果您的应用程序是多用户的,请不要忘记在请求中发送用户 ID,以便 PHP 知道要提取哪些行。
关于php - 将 jQuery Growl 与 PHP 和 MySQL 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2901273/