0

I'm trying to get a .PHP file to run from a .JS file when the JS file runs (and loops every 4 seconds). The code below does not work. I'm wondering if its to do with file linking in WordPress? I'm very new to JS but doing my best to learn.

$(document).ready(function(){
      setInterval(function() {
           alert("Database updated");
          
     var xhttp = new XMLHttpRequest();
     xhttp.open("GET", "tb-user.php", true);
    }, 4000);
});
Mike
  • 1

1 Answers1

0

You should use correct style of http request. Use this.

$(document).ready(function(){
      setInterval(function() {
         alert("Database updated");
          
         var xhttp = new XMLHttpRequest();
         xhttp.open("GET", "tb-user.php", true);
         xhttp.onreadystatechange = function() { 
           if (xhttp.readyState == 4 && xhttp.status == 200)
              console.log(xhttp.responseText);
         }
         xhttp.open("GET", "tb-user.php", true); // true for asynchronous 
         xhttp.send();
        }, 4000);
});