简体   繁体   English

使用AJAX POST到PHP并在更改时刷新

[英]POST to PHP using AJAX and refresh upon change

I am trying to build a web application that needs to refresh the entire page when there is a change in the database. 我正在尝试构建一个Web应用程序,当数据库发生更改时需要刷新整个页面。 I would like to achieve this using AJAX and PHP. 我想用AJAX和PHP来实现这一点。 I would like to POST a single piece of information to the PHP script every 5 seconds and if the returned value from the PHP script is different from a predefined variable, I would like to refresh the entire page. 我想每5秒向PHP脚本发送一条信息,如果PHP脚本返回的值与预定义变量不同,我想刷新整个页面。

For example, I have a predefined value in javascript of 200. If the PHP script returns a different value, I would like to refresh the entire page. 例如,我在javascript为200的预定义值。如果PHP脚本返回不同的值,我想刷新整个页面。

I know how to write the PHP, it is just the XJAX I am having issues with. 我知道如何编写PHP,它只是我遇到问题的XJAX。 I would also not like to use jquery if possible. 如果可能的话,我也不想使用jquery。

Thanks in advance for any guidance! 在此先感谢任何指导!

EDIT : I would not like to use jquery or any other framework, just raw javascript. 编辑:我不想使用jquery或任何其他框架,只是原始的JavaScript。 I also need to refresh the entire page upon change and run the AJAX every 5 seconds. 我还需要在更改时刷新整个页面并每5秒运行一次AJAX。

Good question. 好问题。 You can do if(xmlhttp.responseText !== "<?php echo $currentValue; ?>") { to check if the PHP-side value has changed. 您可以执行if(xmlhttp.responseText !== "<?php echo $currentValue; ?>") {来检查PHP端值是否已更改。 This watchdog method is called very 5 seconds through setInterval . 这个watchdog方法通过setInterval调用非常5秒。 If there is a change, then the page is refreshed via document.location.reload(true) ( ref ) to prevent reusing the cache. 如果有更改,则通过document.location.reload(true)ref )刷新页面以防止重用缓存。

function watchdog() {
  var xmlhttp;
  if (window.XMLHttpRequest){
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {
    // code for IE6, IE5 - whatever, it doesn't hurt
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {

      // This is how you can discover a server-side change
      if(xmlhttp.responseText !== "<?php echo $currentValue; ?>") {
        document.location.reload(true); // Don't reuse cache
      }
    }
  };
  xmlhttp.open("POST","page.php",true);
  xmlhttp.send();
}

// Call watchdog() every 5 seconds
setInterval(function(){ watchdog(); }, 5000);

Note: I chose setInterval over setTimeout because if the connection to the server fails (500-error, timeout, etc.), then the response logic may otherwise fail to trigger another setTimeout . 注意:我在setTimeout选择了setInterval ,因为如果与服务器的连接失败(500错误,超时等),则响应逻辑可能无法触发另一个setTimeout setInterval ensures the server is polled consistently regardless of connection failures. setInterval确保无论连接失败如何都可以一致地轮询服务器。

Your AJAX can be executed every 5 seconds using the setInterval() function. 您可以使用setInterval()函数每5秒执行一次AJAX。 And you can call to refresh the page from AJAX request completion callback. 您可以调用从AJAX请求完成回调刷新页面。

Please try below code. 请尝试下面的代码。 I have used javascript setInterval function which will make ajax call every 5 seconds. 我使用了javascript setInterval函数,它将每5秒进行一次ajax调用。 Now please make efforts to make changes in code as per your requirements and take it further. 现在请努力根据您的要求更改代码并进一步完善。

<script type="text/javascript" >
    var predefined_val = 'test';// your predefined value.
    $.document(ready(function(){
        setInterval(function(){
            $.ajax({
                type:"POST",
                url:"test/test_script.php", //put relative url here, script which will return php
                data:{}, // if any you would like to post any data
                success:function(response){
                    var data = response; // response data from your php script
                    if(predefined_val !== data){
                        // action you want to perform on value changes.
                    }
                }
            });                     
        },5000);// function will run every 5 seconds
    }));
</script>

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

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