异步获取数据库插入数据到前端

2018-12-06  本文已影响6人  张知卓见

目标

现有页面的数据需要刷新网页才能获取到,需要实现自动显示数据到前端。

思路

由于是 ThinkPHP 框架,需要先在 controler 中建立一个纯数据页面,用 M方法 查到数据库的值放在这个页面,这里的查询加了一些必要条件,和不符合条件的页面跳转。

public function mydataonlydata(){
    $myhealth=M("my_health");
    $userId=getSessionUserId();
    $theStatus = 1;
    $row=$myhealth->field("startime,status,afterHeight")->where("memberId='%s' and status=%d",$userId,$theStatus)->order("startime desc")->find();
    if(!$row){
        $this->redirect('Wap/User/bangding');die;
    }
    $afterHeight = $row['afterHeight'];
    echo $afterHeight;
}

setInterval() 方法让纯数据页面每隔10秒刷新一次,refresh() 方法将数据并显示到前端。

$(function () {
  setInterval("refresh()",10000);

})

function refresh() {
    var url="{:U('Wap/User/mydataonlydata')}";
    $.post(url,function(msg){
        getVal(msg);
    })
}

function getVal (msg) {
    var afterHeight = msg;
    var ah = document.getElementById("ah");
    ah.innerHTML = afterHeight;
}
上一篇下一篇

猜你喜欢

热点阅读