dongli8466 2015-10-14 07:45
浏览 56

php中的ajax新记录计数通知

iam doing a page in php that if any new record is entered it will notify the users screen with the new record count. Following is the code i did for the same, but its not working fine. Can u pls suggest me as of what iam doing wrong...

 alert.php

 <?php
 require("config.php");
 $result = mysql_query("SELECT * FROM marketing_tend_corr");
 $res = mysql_num_rows($result);
 echo $res;
 ?>




 index.php
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
 <?php 
 define('BASEPATH', true);
 require("config.php");
 ?>
 <script>

 var count_cases = -1;
 setInterval(function(){    
    $.ajax({
    type : "POST",
    url : "alert.php",
    success : function(response){
        if (count_cases != -1 && count_cases != response) echo $count_cases);
        count_cases = response;
    }
});
 },1000);

 </script>
  • 写回答

1条回答 默认 最新

  • dsk88199 2015-10-14 16:52
    关注

    The following line of code is not going to work in Javascript:

    if (count_cases != -1 && count_cases != response) echo $count_cases);
    

    This line of code contains php code (echo $count_cases) which is server side code.

    I've changed the code a bit and replaced the number of records by returning a random value.

    // alert.php

    <?php
    
    echo rand(1, 1000000);
    

    //index.php

    <!DOCTYPE html>
    <html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
    
            setInterval(function(){
                $.ajax({
                    type : "POST",
                    url : "alert.php",
                    success : function(response){                        
                        $("body").html(response);
                    }
                });
            },1000);
    
        </script>
    </head>
    <body>
    </body>
    </html>
    

    You can check the index.php file in your browser to see the random numbers being returned. This random number should in your case become the result of your 'mysql_num_rows' function.

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改