gpt4 book ai didi

php - 如果使用 php ajax 添加新用户,管理面板菜单上的通知气泡?

转载 作者:行者123 更新时间:2023-11-29 05:14:41 27 4
gpt4 key购买 nike

如何在查看/单击菜单气泡消失后使用 Ajax php 在我的管理菜单中添加像 Facebook 这样的通知气泡(当新用户注册时)。有人帮忙吗???

最佳答案

当用户注册时,我猜你将他/她添加到你的数据库中?如果是这样,我会在您的 users 中添加一个字段名为“notificationViewed”的数据库,即 false默认情况下,当您将该用户放入数据库时​​。

当您连接或刷新您的管理菜单页面时,如果任何用户有一个字段notificationViewed == false,您为该页面提供服务的 php 应该检查数据库。 , 和 COUNT此类返回用户的数量。在代表小泡泡的 html 标签中,添加一个属性 data-newUsers="<?= COUNT_OF_NEW_USERS ?>" .


现在在客户端...

比方说,id="bubble"默认情况下使用 CSS 隐藏:

#bubble {
display:none;
}

使用 JavaScript,您可以访问 data-*轻松属性:

var newUsers = document.getElementById('bubble').dataset.newUsers; // holds the number

或使用 jQuery:

var newUsers = $('#bubble').data('newUsers'); // same thing

此时,您可以检查是否newUsers > 0 .如果是这样,请用数字填充气泡(如果需要),然后做一个漂亮的 fadeIn动画片。 jQuery 中的示例:

if (newUsers > 0) {
$('bubble').text(newUsers).fadeIn();
}

现在,我们希望能够检测到点击气泡的时间,以便隐藏气泡并丢弃新注册的用户。同样,使用 jQuery:

$('#bubble').click(function() {
$.post('discardNotifications.php', {usersNotified: newUsers}, function(data) {
if (data === "ok") { // php script returns this string if all went right
$('#bubble').fadeOut(function() {
$('#bubble').remove(); // remove the element from the DOM, to prevent further clicks of the element
}); // nice fadeOut animation of the bubble
}
}
});

函数只有在 POST 时才会被调用请求成功。 POST请求定向到 discardNotifications.php ,它必须与您的管理菜单 html 文件位于同一目录中(如果不是,只需更改相对路径)。调用的第二个参数是一个垃圾对象,其中包含通知的新用户数量,它会发送到您的后端。


回到后端,里面 discardNotifications.php ...

你必须检查是否有 POST参数名为“usersNotified”,然后查询您的 users数据库并最多更新“usersNotified”给出的数字。这考虑到自您刷新管理页面后可能有新用户订阅,并且您希望收到这些新用户的通知。不选择最多的“usersNotified”可能会忽略它们。示例(但不完整):

if (isset($_POST['usersNotified']))
{
$number = $_POST['usersNotified'];

// update the "notificationViewed" field to TRUE for at most $number different users

echo "ok"; // everything went right
} else {
echo "bad";
}

显然您可以进行一些更改,并且您必须实现一些数据库处理。告诉我它是否有效!

Ps:我的代码片段可能会有一些小错误,我没有测试所有内容。

关于php - 如果使用 php ajax 添加新用户,管理面板菜单上的通知气泡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34702939/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com