通过ajax在网站上提醒用户

时间:2016-01-22 17:26:02

标签: javascript php jquery 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));

?>

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

这也适用于加载到页面中的第一个用户,但在此之后,显然数据库中没有更多'notify = 0',因为它们已经更新,因此它们不会显示通知。< / p>

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

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)

我真的被困在这里。

2 个答案:

答案 0 :(得分:0)

您可以使用websockets或socket.io来创建它。 然后,多个客户端的js将与该套接字连接,将通知所有连接的人他们的票据

答案 1 :(得分:0)

您可以将更新语句更改为:

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