首页 > 解决方案 > 如何使用 AJAX 刷新 div?

问题描述

我知道有很多关于这个主题的条目,但我没有让我的代码工作。我构建了一个 div 容器(这是我想要每秒刷新的 div),其中包括 3 个其他 div 容器。

到目前为止我的代码:index.php

<div id="ajax">
   <div class="row d-flex align-items-center">
      <div class="showPGF"> 
         <?php
            $controller->getEntries();
         ?>
      </div>
   </div>

   <div class="row d-flex align-items-center">
      <div class="showNGi_sample"> 
         <?php
            $controller->getUsers();
         ?>
      </div>
   </div>

   <br>

   <div class="row d-flex align-items-center">
      <div class="showNGi_sample"> 
         <?php
            $controller->getPGFEntries();
         ?>
      </div>
   </div>
</div>

脚本.js

$(document).ready(function () {

function ajax(){
        $('#ajax').load('index.php',function() {
            $(this).unwrap();
        });
    }

    ajax(); // This will run on page load
    setInterval(function(){
        ajax() // this will run after every 1 seconds
    }, 1000);

}

我想获得具有“ajax”ID 的 DIV 以每秒重新加载。因为我想始终从我的数据库中获取当前的条目/用户(每秒执行一次 php 调用)。但目前它不刷新,我需要按 F5 才能显示新条目。

标签: javascriptphpjqueryhtmlajax

解决方案


推荐阅读