打开下拉菜单时更新 MySQL 数据库(注意)

Update MySQL database when a dropdown menu is opened (NB)

我有一个通知下拉列表(如 facebook),它显示存储在我的数据库中的通知。

我想在用户下拉通知时将通知标记为已读 "menu"。这可能吗?如果可以,谁能帮我做这件事?

数据库将信息存储在字段 'notification_read' 中,该字段设置为 0 或 1(0 表示未读)。

谢谢,抱歉我是NB

我的下拉菜单代码示例

<ul class="dropdown-menu notifications arrow">
                    <li class="dd-header">
                        <span>You have 3 new notification(s)</span>
                        <span><a href="#">Mark all Seen</a></span>
                    </li>
                    <div class="scrollthis">
                        <li>
                            <a href="#" class="notification-user active">
                                <span class="time">4 mins</span>
                                <i class="fa fa-user"></i>
                                <span class="msg">New user Registered. </span>
                            </a>
                        </li>

无需详细说明(因为这很宽泛而且我不知道您的代码结构),您基本上需要两件事:

  1. 更新数据库的服务器端脚本。这可以接受更新内容的参数,或者它可以只是一个全局操作,将所有项目标记为当前用户已读。 (我假设是后者。)
  2. 通过 AJAX.
  3. 调用服务器端脚本的客户端代码

对于服务器端脚本,为简单起见,它将是一个单独的独立 "page"。如果我们假设没有参数并且它可以从会话中推断出当前用户,那么这个脚本需要做的就是执行您的 SQL 代码来执行更新。然后它可以 return(嗯,"print to the page")像 truefalse 这样简单的东西来指示更新是否成功。 (如果不成功,请务必在某处记录错误,以便诊断问题。)

我们假设这叫做 markAsRead.php

现在对于客户端脚本,可以像这样简单(假设 jQuery,因为徒手编写更容易):

$.get('markAsRead.php');

或者如果客户端代码需要以某种方式响应,例如从菜单更改样式:

$.get('markAsRead.php', function (data) {
    // examine data for the response, which from the above
    // description could just be a "true" or "false", and
    // perform client-side logic based on that response
});

此客户端代码只会在您使用的任何处理程序中调用。例如,如果用户点击了一些名为 #messagesdiv,它可能看起来像这样:

$('#messages').click(function () {
    $.get('markAsRead.php', function (data) {
        // examine data for the response, which from the above
        // description could just be a "true" or "false", and
        // perform client-side logic based on that response
    });
});

总的来说,重点是客户端代码响应 UI 事件(点击),向服务器端资源发送请求,服务器端资源依次执行服务器端逻辑,然后读取来自该资源的响应以执行任何其他客户端逻辑。