douli4852 2019-03-22 23:05
浏览 59

在AJAX刷新时检测到新注释时播放声音

I'm making a live chat system for my site. Any help with solving this issue is greatly appreciated =). I have a page ticket.php with a div #reply-holder where I fetch the comments for that ticket using 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>   

My ajax.php page where the comments are fetched from the MYSQL db and display is:

<?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;
                }

            ?>

I've tried the following code, but the sound isn't working, no sound is played even if a new comment is inserted.. I first tried it on ajax.php but it would just play the sound after every refresh even if no new comment was inserted.

I'm pretty sure the code is alright, I just don't know exactly where to place it.

<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题