我希望在$ .post中实时更新而无需重新加载或刷新

时间:2013-04-15 06:14:10

标签: php jquery mysql ajax .post

这是我的PHP代码_

if(isset($_REQUEST['show']))
{
        $con=mysqli_connect("localhost","root","","server_name");

        $show = mysqli_query($con, "SELECT * FROM name1 ORDER BY date DESC");
        $html='';
        while($showE = mysqli_fetch_array($show)) 
        {
        $html.= '<h3>'.$showE['un_name'].'</h3>';
        $html.= '<div>'.$showE['un_name_dec'].'</div>';
        }
        echo $html;
       //End of while loop

}

这是jquery代码_

$.post('preprocessor.php', '&show=', function(data) {
    $("#accordion").html(data);
    $("#accordion").accordion({
       collapsible: true,
       icons: {
            activeHeader: "ui-icon-triangle-1-s",
            header: "ui-icon-triangle-1-e"
        }
    });
    });

这是身体_

<div id='accordion'>
</div>  

我没有得到实时更新,一切正常,但Ajax的东西不起作用...如果我刷新或重新加载页面但它本身不起作用

4 个答案:

答案 0 :(得分:0)

您要找的是setTimeout

 $("#accordion").accordion({
       collapsible: true,
       icons: {
            activeHeader: "ui-icon-triangle-1-s",
            header: "ui-icon-triangle-1-e"
        }
    });
setInterval(makePostCall, 15000); // decide interval to fetch new updates. 15 sec for example

function makePostCall(){
$.post('preprocessor.php', '&show=', function(data) {
    $("#accordion").html(data);

    });
}

但这不是做这种事情的正确方法。考虑使用Ajax Push Engine

答案 1 :(得分:0)

您是否设置间隔以特定间隔调用代码?你需要做的就是, 在JavaScript上添加:

 setinterval(function(){/* do the post here */ },1000 /*this is time */);

答案 2 :(得分:0)

如果我刷新或重新加载页面但它本身不起作用

为此你需要使用setInterval();函数来检查给定的每个特定时间段:

   setInterval(function(){
      $.post('preprocessor.php', '&show=', function(data) {
         $("#accordion").html(data);
         $("#accordion").accordion({
                   collapsible: true,
                   icons: {
                   activeHeader: "ui-icon-triangle-1-s",
                   header: "ui-icon-triangle-1-e"
                   }
         });
      });
   },5000); //<---- runs every 5000 ms 

这将每5秒运行一次,并将更新#accordion div。

答案 3 :(得分:0)

尝试:

$.post('preprocessor.php', {show:''}, function(data) {
$("#accordion").html(data);
$("#accordion").accordion({
   collapsible: true,
   icons: {
        activeHeader: "ui-icon-triangle-1-s",
        header: "ui-icon-triangle-1-e"
    }
});
},'html');