0
var myVar;
var tone = new Audio('../assets/sound/signal100.ogg');
var signal;

signal = $.get('otherPage.html');

function myFunction() {
    myVar = setInterval(signalCheck, 1000);
}

function signalCheck() {
    var signal = <?php  $query = $db->query("SELECT * FROM `servers` WHERE `id` = '1'");
  $data = $query->fetch_array(); echo $data['radioStatus']; ?>;
if(signal == 100) {
  document.getElementById("signal100").innerHTML = '<div class="alert alert-    danger" role="alert" style="text-align: center; height: 50px;"><p><b>Function Triggered! &nbsp;<a href="#" data-    toggle="modal" data-target="#liftSignal">Lift signal</a></p></div>';
            tone.play();
            setInterval(myFunction, 1000);
              } else {
                document.getElementById("signal").innerHTML = "";
                tone.stop();
          }
}

window.onload = myFunction();

This is what I am using currently. It all works fine, only I have to reload the page; something I want to prevent. I want to have the script check the query and if the value I want to see changed has changed. When it changes to 100, it should play my sound and pop up my banner. If I set my database valuable to 100 and reload the page, it goes fine, only when I set it back to 0 or whatever value, I have to reload the page to see the effect.

What am I doing wrong?

danielr
  • 51
  • 6
  • You mistakenly think that your PHP code will be executed again when the JS function `signalCheck` is called, but that is not true. The output that PHP generated there is hard-coded in the function. The server does not participate with that code any more. Check how the function code looks in your browser (view HTML source), and you will see: there is no PHP code there. – trincot Jan 28 '18 at 18:40
  • Thanks, @trincot , I did know this but I went wrong thinking that it would just run the query again when the interval restarted the script. What would you suggest? – danielr Jan 28 '18 at 18:41
  • The interval does not restart the (PHP) script. See the duplicate (ignore the accepted answer there). You need to make an ajax call. NB: Added a few more references. – trincot Jan 28 '18 at 18:43

0 Answers0