通过 ajax 在网站上提醒用户


Alerting users on website through ajax

我正在尝试制作一个通知系统,当用户被分配到工单或将新工单添加到数据库时通知用户。

我已经拥有的系统本身可以工作,只是它只将通知发送给收到 ajax 请求的第一个用户。有没有办法让每个应该收到通知的人都真正收到通知?

我的代码:JavaScript:

        function checkUpdates()
            {
                $.ajax({
                    type: "POST",
                    url: 'ajax/checkDB.php',   // a webservice or other URL that queries the database
                    data: {},
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data) {
                        console.log('Connected and executed PHP page... '+data);
                        if (data.hasChanged == "true") {
                            playSound('img/notif2');
                            notifyAdmin();
                            console.log('Updated Tickets Page...');
                            $("#contents").load("dynamic/tickets.php");
                            $("#contents").fadeTo("fast", 1);
                        }
                        if (data.newAssigned == "true") {
                            playSound('img/notif2');
                            notifyUser();
                            console.log('Updated Tickets Page...');
                            $("#contents").load("dynamic/tickets.php");
                            $("#contents").fadeTo("fast", 1);
                        }
                    }
                 });
            }
        $(document).ready(function() {
            setInterval("checkUpdates()", 3000);  // Calls the function every 3 seconds
        });

我的php脚本(checkDB.php):

<?php
include("../static/config.php");
session_start();
header("Content-Type: text/json");
        $result = mysqli_query($con,"SELECT notify FROM tickets WHERE notify='0'");
        $row_cnt = mysqli_num_rows($result);
        if($row_cnt > 0) {
            $hasChanged = 'true';
            mysqli_query($con,"UPDATE tickets SET notify='1' WHERE notify='0'");
        } else {
            $hasChanged = 'false';
        }
        $result2 = mysqli_query($con,"SELECT notify,Assigned FROM tickets WHERE Completeness='1' AND notify='1'");
        $row_cnt2 = mysqli_num_rows($result2);
        if($row_cnt2 > 0) {
            while($row2 = mysqli_fetch_array($result2))
                {
                    if(strcmp($_SESSION['Name'],$row2['Assigned']) == 0) {
                        $newAssigned = 'true';
                    } else {
                        $newAssigned = 'false';
                    }
                }
            mysqli_query($con,"UPDATE tickets SET notify='2' WHERE Completeness='1' AND notify='1'");
        } else {
            $newAssigned = 'false';
        }
        echo json_encode(array('newAssigned' => $newAssigned, 'hasChanged' => $hasChanged));
?>

以下是我的确切意图的概要:用户登录到系统,如果授予管理权限,则被授予管理权限。用户加载工单页面,其中列出了所有工单以及我给你的这个 javascriptJavaScript 每 3 秒加载一次 checkDB.php基本上是对数据库运行 'notify' 值为 0 的检查,如果至少存在 1,则在数据库中更新为 '1' 并返回为 true 给 ajax 调用 - 它发出通知。

同样,这适用于加载到页面中的第一个用户,但在那之后,显然数据库中不再有"notify=0",因为它们已经更新,因此不会显示通知。

有人要求我共享我的数据库结构。票:

UniqueID - Unique ID per ticket (always unique)
Requester - Whoever submitted the ticket.
Problem - Description of the given issue.
Assigned - User who is assigned to the ticket.
Completeness - The level of completeness (0-4)
Times - Times listed for ticket start, assigned, etc
notified - Used in old ticket system (I plan to get rid of it)
Urgency - Selected by requester, how urgent the ticket is
Location - Location in our warehouse assistance is needed.
FollowUp - currently not in use, will be used to submit follow ups.
isProject - (0-1) is project or not
additionalTechs - Lists additional users on a ticket (Not important for question)
notify - (0-2) at 0, ticket is new and notif should be sent to admins which should set this to 1, when it is 1 it should send a notif to users that are attached to the ticket.

技术数据库:

UniqueID
Username
Password
Name
Level
Disabled
LastTimeSeen
needsNotify (Used in old system, plan to remove)

我真的被困在这里了。

你可以

用websockets或 socket.io 来制作。然后多个客户端的js将与该套接字连接,将通知所有连接的人有关他们的票证

您可以将更新语句更改为以下内容:

UPDATE tickets SET notify='1' WHERE notify='0' and Assigned = ''//some user id passed