# dedecms如何通过AJAX实现PHP自动检测订单数量，并发送语音提示

*Published:* 2023-11-14
*Author:* 客服001

#### 温馨提醒

如果文章内容或图片资源失效，请留言反馈，我们会及时处理，谢谢

本文最后更新于2023年11月14日，已超过 180天没有更新



使用场景：
-----

所涉网站启用了会员功能，部分内容需要升级VIP会员才可以，所以需要在网站后台增加一个对VIP会员数据的检测，当有新订单创建的时候，自动发送语音提醒。

本次涉及的数据表为#@\_\_member\_operation。

实现思路：
-----

通过AJAX定时检测指定的表，如果检测到符合的数据，就执行播放语音的操作。

具体步骤：
-----

一、在**dede/templets/index2.htm**页面底部增加如下代码：

```
<!--支付成功声音提示-->
<script type="text/javascript">
    function hello() {
        $.<a class="tag_link" href="https://www.xarjtc.com/tag/ajax" target="_blank" title="查看ajax此标签更多文章"><abbr title="">ajax</abbr></a>({
            url: '/dede/getOrder.php',
            type: 'get',
            datatype: 'text',
            async: false,
            success: function(result) {
                if (result == 200) {
                    playSound();
                }
            }
        });
    }
    setInterval("hello()", 10000); // 10秒刷新一次 window.onbeforeunload = function (e) { var message = 'some word'; e = e || window.event; if (e) { e.returnValue = message; } clearInterval() }; 
    
</script>
<script>
    var playSound = function(msgfile = "/images/voice.mp3") {
        var borswer = window.navigator.userAgent.toLowerCase();
        if (borswer.indexOf("ie") >= 0) { //IE内核浏览器 var strEmbed = '<embed name="embedPlay" src="'+msgfile+'" autostart="true" hidden="true" loop="false"></embed>'; if ( $( "body" ).find( "embed" ).length <= 0 ) $( "body" ).append( strEmbed ); var embed = document.embedPlay; //浏览器不支持 audion，则使用 embed 播放 embed.volume = 100; //embed.play();这个不需要 } else { //非IE内核浏览器 var strAudio = "<audio id='audioPlay' src='"+msgfile+"' hidden='true'>"; if($("#audioPlay").length<=0){ $( "body" ).append( strAudio ); } var audio = document.getElementById( "audioPlay" ); //浏览器支持 audio audio.play(); } } 
            
</script>
```

二、新增文件**dede/getOrder.php**，具体代码如下：

```
<?php 
/** * 订单检测管理 * * @writer zhimatong * @time 2021-12-19 */
require_once(dirname(__FILE__).'/config.php');
require_once(DEDEINC.'/common.func.php');
$row = $dsql->GetOne("SELECT * FROM `#@__member_operation` WHERE sta=0 and (mtime <= (now() - 1000*60*30))");
//30分钟内有新订单提醒 if(is_array($row)) { echo 200; }else{ echo 100; } exit();
```

这样就可以了，当30分钟内有新的未完成的订单时就会自动语音消息提醒。如果只检测付款成功的，可以将查询语句改成sta=1。