在AJAX刷新上检测到新评论时播放声音

时间:2019-03-22 23:05:29

标签: php ajax

我正在为我的站点制作实时聊天系统。非常感谢您解决此问题的任何帮助=)。我有一个带有div#reply-holder的页面ticket.php,在其中我使用AJAX获取该票据的注释:

<script type="text/javascript"> 
function loadlink(){
    $('#reply-holder').load('ajax.php',function () {
         $(this).unwrap();
    });
}

loadlink(); // This will run on page load
setInterval(function(){
    loadlink() // this will run after every 5 seconds
}, 15000);
</script>   

我的ajax.php页面是从MYSQL数据库中获取注释并显示的:

<?php
require_once 'inc/config.php';

session_start();
$ticketid = $_SESSION['ticketid'];
$username = $_SESSION['username'];

$getticketid = "SELECT * FROM orders where id = '".$ticketid."' ORDER BY id DESC LIMIT 1"
    or die(mysql_error());

$result = $link->query($getticketid);

$row = $result->fetch_array(MYSQLI_NUM);
$active = $row[2];
$created_at = $row[3];
$message = $row[4];
$price = $row[5];

?>

            <?php

                $getcomments = "SELECT * FROM comments where orderid = ".$ticketid." ORDER BY id DESC"
                    or die(mysql_error());

                $result2 = $link->query($getcomments);
                while($row2 = mysqli_fetch_array($result2)){
                    echo '<div class="reply">';
                    echo '<p>'.$row2[2].' replied:</p>';
                    echo '<h3>'.$row2[3].'</h3></div>';
                    $lastMessageIdWePlayedSoundFor = $row2[0] - 1;
                }

            ?>

我尝试了以下代码,但是声音不起作用,即使插入了新注释也没有声音播放。.我首先在ajax.php上尝试过,但是每次刷新后它只会播放声音即使没有插入新评论。

我很确定代码是否正确,我只是不知道确切的位置。

<?php 

                    $playsound = "SELECT COUNT(*) FROM comments where orderid = '".$ticketid."' ORDER BY id DESC LIMIT 1"
                        or die(mysql_error());

                    $result2 = $link->query($playsound);

                    $row2 = $result2->fetch_array(MYSQLI_NUM);
                    $lastMessageId = $row2[0];

                    if($lastMessageId!=$lastMessageIdWePlayedSoundFor)
                    {
                       $myAudioFile = "notification.mp3";
                        echo '<EMBED SRC="'.$myAudioFile.'" HIDDEN="TRUE" AUTOSTART="TRUE"></EMBED>';
                       $lastMessageIdWePlayedSoundFor = $lastMessageId;
                    }

                ?>

0 个答案:

没有答案