Javascript 从数据库中获取数据并显示在页面上的长轮询

Javascript 从数据库中获取数据并显示在页面上的长轮询,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我想从db中获取前三个值并显示在页面上。显示器的内容应每1小时刷新一次 invite.php: <html> <head></head> <body> <div class="page-header header"> <h1 class="page-header-text">thenwat</h1> &

我想从db中获取前三个值并显示在页面上。显示器的内容应每1小时刷新一次

invite.php:

<html>    
    <head></head>    
    <body>    
        <div class="page-header header">    
            <h1 class="page-header-text">thenwat</h1>    
        </div>    
            <tble border="0" width="100%">    
                <tr>    
                <div class="middle">    
                        <td style="width:60%">    
                            <div>       
                <div id="show">
                    url1.com - sentiment value <br>
                    url2.com - sentiment value <br>
                    url3.com - sentiment value <br>
                </div>    
            </div>                                             

                        </td>                   
                    </div>    
                </tr>    
            </table>   
    </body> 
    <script type="text/javascript">
        var id =123;
        function doIt(){   // I want this functoin to be called after 5 second of the page load
            $("#show").load("refresh.php");  // How to post id to refresh.php page here?
        }
        $(document).ready(function(){
            timeoutId = setTimeout(function(){
                doIt();
                intervalId = setInterval(function(){
                    doIt();
                }, 5000); //Request the doIt() method every 5ms.
            }, 3000); //Delay calculated on the server to trigger the function at the appropriate time
        }); 
    </script>   
</html>

森瓦特
url1.com-情感价值
url2.com-情感价值
url3.com-情感价值
var-id=123; 函数doIt(){//我希望在页面加载5秒后调用此functoid $(“#show”).load(“refresh.php”);//如何在此处将id发布到refresh.php页面? } $(文档).ready(函数(){ timeoutId=setTimeout(函数(){ doIt(); intervalId=setInterval(函数(){ doIt(); },5000);//每5毫秒请求一次doIt()方法。 },3000);//在服务器上计算延迟,以便在适当的时间触发函数 });
refresh.php:

<?php               
        $id=$_POST['id'];
        $con = mysqli_connect('127.0.0.1', 'root', '', 'karim');
        if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            return;
        }
        $insertQuery1 = "SELECT url,sentiment, count(url) from userpost where userid='".$userid."' group by url order by count(url) desc limit 3";

        //Get top thee url with their sentiment value
        // This url would be displayed on invite.php page
        if (!mysqli_query($con,$insertQuery1))
            {
                die('Error: ' . mysqli_error($con));
            }
?>

1.如何将id发布到fresh.php

2.如何在invite.php-show div上显示从refresh.php获取的记录

3.如何将前三名记录从refresh.php发送到invite.php

对于这三个问题,你可以做一个单一的解决方案

使用

在invite.php中,使用如下所示的ajax

$(document).ready(function(){
  setInterval("getData()",3600000);//Polls in every 1 hour
});

function getData(){
    var id =123;
    $.ajax({
              url       : "refresh.php",
              type      : "POST",
              data      : {"id" : id},
              dataType  : "json",
              success   : function(data) {
                 var response    = JSON.stringify(data);
                 res             = JSON.parse(response);
                 console.log(res);//To see the response in browser console
                 //palce the contents in show div
              }
          });
}
invite.php中的一个

执行向ajax发送响应的代码。然后将响应作为json编码的响应发送。查看更多信息