美文网首页
异步获取数据库插入数据到前端

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

作者: 张知卓见 | 来源:发表于2018-12-06 17:02 被阅读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;
    }
    

    相关文章

      网友评论

          本文标题:异步获取数据库插入数据到前端

          本文链接:https://www.haomeiwen.com/subject/dbbocqtx.html