长轮询从db获取数据并在页面上显示

时间:2013-12-20 07:56:18

标签: javascript php jquery html

我想从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>

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上从refresh.php获取的diplay记录 - show div
  3. 如何从refresh.php发送前三条记录到invite.php

1 个答案:

答案 0 :(得分:1)

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

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

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

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

使用AJAX

在你的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编码的响应发送响应。检查json_encode()以获取更多信息。