-1

I wanna see if a value in a table has changed in a loop. there is a 60 second timer and while it counts it keeps checking if the table changed. here is the code:

setTimeout(function(x){return function(){
        clabel.innerHTML = "Waiting " + x.toString() + "/ 60 seconds. Checker is "+ c;
        c = checkdb();
        if(c == 2){
            window.location = "http://www.google.com";  
        }
        };}(i), 1000*i);
function checkdb(){
    return  <?php
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $stmt = $conn->prepare('SELECT * FROM game');
            $stmt->execute(array('gameID' => $gameID));
            $check->execute();
            $r = $check->fetchColumn(1);
            $conn = null;
            echo $r; 
    ?>;

when i change the table while the counter is going , the page does not get redirected. even though when i manually check the table the value does change there.

shintax
  • 11
  • 2

1 Answers1

0

As @xNeyte mentioned above, you're trying to put PHP code into Javascript container. Keep in mind that Javascript is client-side meanwhile PHP is server side. If you need to access asynchroneously the server from your Javascript file, then you should have a look at Ajax which does provide this kind of feature.

This way, your script would be divided in 3 parts :

  1. the basic PHP script which display your javascript

  2. the javascript part which will use Ajax to make an async. request to another PHP file

  3. another PHP script that will return you what you need (what you've put in your last block)

Cr3aHal0
  • 809
  • 4
  • 11