0

this is my code for a simple chatbox basically what does this does is on clicking send(input) button it will send the message into database and also create notification for the reciever but I want to create notification only when the user is not the this this page i.e on /message.php. Is there any way I can know on which the other user is? or any other approach to what I am trying to do?

$(".chatbox input").on("click", function () {
    var rly = $("#reply").val();
    var send = "<?php echo $Sender?>";

    $.ajax({
        type: 'POST',
        url: 'Message_chat.php',
        data: {
            repl: rly
        },
        dataType: 'json',
        success: function (data) {
            document.getElementById("reply").value = "";
            populatedata(data, send);
        },
        error: e => console.log(e)
    });
});
function fetchdata() {
    var send = "<?php echo $Sender?>";

    $.ajax({
        type: 'GET',
        url: 'Message_chat.php',
        dataType: 'json',
        success: function (data) {
            populatedata(data, send);
            notification();
        }
    });
}
function notification() {
    var notify = "";
    $.ajax({
        type: 'GET',
        url: 'notify.php',
        dataType: 'json',
        success: function (data) {
            if (data == null)
            {
                document.getElementsByClassName("ncircle")[0].style.display = "none";
            } else {
                var len = data.length;
                for (var i = 0; i < len; i++) {
                    if (data[i].nread == 0) {
                        notify += "<p>" + data[i].nby + " sent you a message</p>";
                    }
                }
                $("#myDropdown").html(notify);
                if (data.length != 0) {
                    document.getElementsByClassName("ncircle")[0].style.display = "block";
                    $(".ncircle").html(len);
                }
            }
        },
        error: e => console.log(e)
    });
}
Styx
  • 9,863
  • 8
  • 43
  • 53
  • You can maintain some record in database and show user notification when user loads the page or you can use `window.location` object in "notification" function to check current url https://stackoverflow.com/questions/406192/get-current-url-in-javascript – hanish singla Sep 29 '17 at 05:20
  • If it's a PHP site, simply do not include that script on the chat page... – Salketer Sep 29 '17 at 05:58
  • You store user's last url in database. so when you send message at that time check if user is on message page than not send notification otherwise send notification to user. – Pankaj Sharma Sep 29 '17 at 06:05
  • I guess this solution would work i will try tommorrow thanks anyways – Karan S Warraich Sep 29 '17 at 06:07

0 Answers0