0

I'm trying to use a code to set a MySQL flag to true from a PHP file (Let's say A). I would like to read this MySQL flag from another PHP file (B), that will be already opened. The question is: Is there any way to manually refresh this (B) page after changing the flag value from the first page (A)?

May be using cron or something similar, I don't really want to refresh page B every X seconds until it reads new flag value.

rtruszk
  • 3,902
  • 13
  • 36
  • 53
  • http://stackoverflow.com/questions/10028770/html5-websocket-vs-long-polling-vs-ajax-vs-webrtc-vs-server-sent-events –  Apr 13 '15 at 21:50

1 Answers1

0

Use AJAX.

Inside B page create a request function to check the status of MySQL flag. If the flag is set to true, refresh the page.

In HTML of B program insert:

<head>
...
  <script>
    function enableChecker() {
        setInterval( checkFlag, 10000); // Check each ten seconds
    }

    function checkFlag() {
        xmlhttp = GetXmlHttpObject();
        if ( xmlhttp==null ) return;
        xmlhttp.onreadystatechange = function() {
            if ( xmlhttp.readyState == 4 ) {
                if ( xmlhttp.responseText == "OK" ) {
                    location.reload(); // Refresh the page
                }
            }
        }
        xmlhttp.open( 'GET', 'myCheckProgram.php', true ); // Call php program to check the flag value
        xmlhttp.send( null );
        return false;
    }
  </script>
</head>
<body onload="enableChecker()" >

Create a program called myCheckProgram.php

<?php

/* Blah blah to connect with database and query for flag */

$flag = // Result of query

echo $flag ? 'OK' : 'NOK' // Return OK if flag is true

?>