简体   繁体   English

PHP-提取数据时的Ajax自动刷新页面

[英]PHP - Ajax Auto Refresh Page when Data is Fetch

Here I have a set interval , which I think will reload the page automatically when data is fetched. 在这里,我有一个设置的时间间隔 ,我认为它将在获取数据时自动重新加载页面。

here please look my UI and how am i displaying the modal without refresh,and i have database connection so there is a basis on how i will trigger the modal when i submit the button it will insert an id and that id will be save in the modal so the modal will trigger and display . 在这里,请查看我的UI以及我如何显示不刷新的模式,并且我具有数据库连接,所以有一个基于我提交按钮时将如何触发模式的信息,它将插入一个id并且该id将保存在模态,所以模态将触发显示

clickModal.php - here is where the button should click and will trigger the modal and display it without refresh on my home.php clickModal.php这是按钮应单击的位置,它将触发模式并在不刷新的情况下在home.php上显示它

点击模型

home.php - here is where the modal should popup or display when I click the button on clickModal.php home.php这是我单击clickModal.php上的按钮时应该弹出或显示模式的clickModal.php

for now it is working but I need to refresh the page on home.php before the modal will display or pop up here is my code. 现在它正在工作,但是我需要刷新 home.php上的页面,然后模态才会显示或弹出,这是我的代码。

clickModal.php button click / ajax success. clickModal.php按钮单击/ ajax成功。

            Yes: 
                                        btnClass: 'btn-green',
                                        action: function () {
                                            $.ajax({
                                                type: "POST",
                                                url: "announcement.php",
                                                data: {
                                                    addInfo: addInfo
                                                },
                                                dataType: "text",
                                                success: function (data) {
                                                    window.location.replace("change-password.php");

                                                },
                                                error: function (err) {
                                                    console.log(err);

                                                }

                                            });

                                        }

home.php

my database connection 我的数据库连接

 $data = mysqli_query($con,"SELECT * FROM announcement");
  $count = mysqli_num_rows($data);

my script 我的剧本

    <script>


var still_fetching = false;

//fetch data every 3 seconds (3000)
setInterval(function(){ 
     if (still_fetching) {
         return;
     }
     still_fetching = true;
     loadUsers();
}, 3000);

//need to update a bit this function
function loadUsers(){
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'home.php', true);

        xhr.onload = function(){
            if(this.status == 200){
                var users = JSON.parse(this.responseText);

                var output = '';

                for(var i in users){
                output += '<div id="myModal" class="modal" style="position:fixed;  display: none; padding-top: 100px;'+
                'left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.9); ">'+
                '<div class="container" style="width: 100%">'+
                '<div class="card col-12" style="background-color: red; color: white;">'+
                '<div class="card-header">'+
                    '<p><h3 class="text-center">Announcement</h3></p>'+
                '</div>'+
                  '<div class="card-body text-center">'+

                      '<p>Please click refresh button to resume after the announcement</p>'+
                  '</div>'+
                 '</div>'+
                '</div>'+
                '<img class="modal-content" id="img01">'+
                '</div>';
                }

                 document.getElementById('myModal').style.display='block';
                still_fetching = false;
            }
        }

        xhr.send();
  }



</script>

what I just need is to display the modal without any refresh when it is triggered. 我需要的是在触发模式时不进行任何刷新就显示模式。

So your question is to open the modal once your Ajax call responded: 因此,您的问题是在您的Ajax调用响应后打开模式:

Your Ajax call should be: 您的Ajax电话应该​​是:

$.get("youpage.php",{ bar: "foo"}, function(data, status){
    // Trigger your modal
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM