0

I have set up series of pages whose display order depends on a changing variable from a common database. Each page posts a value to the database when loaded (ex: page A posts value 1), then begins to count down.

When a user on a different machine clicks on another page, the value is updated (page B posts value 2).

I would like to make it so that page A could read the change in value while it's still counting down and, without refreshing, could redirect itself to another page, let's say C.

Since I already have a PHP script calling the database to update the value, I can't repeat that call without changing the value from what B posted (2) back to (1).

I know I need use AJAX and setinterval(), and I've looked through many, many examples, but I can't seem to find a way to have page A retrieve the value and activate a redirect that doesn't include printing the value in some way (I'm not actually showing the value on the page) or constructing a div (as in HTML - Change\Update page contents without refreshing\reloading the page).

My thinking is that I need to create an external PHP file to query the database and use AJAX to call it repeatedly and pass the updated value to a refresh function in some way.

I'm fairly new and a little clumsy at this so any advice would be appreciated.

Here is the code I am using to initially call the database (which has only one line) and update the value.

<?php

mysql_connect("xxxx", "xxxx", "xxxx") or die;

mysql_select_db("xxxx");

$result = mysql_query("SELECT * FROM database");
$row = mysql_fetch_assoc($result);
$value = $row ['value'];

if ($value == "0"){

mysql_query("UPDATE gym SET value = '1' WHERE id = '1'");           

?>
Community
  • 1
  • 1
NokoB
  • 11
  • 4

1 Answers1

0
  1. Set up an additional PHP file which will query the database and return the value of the query.
  2. Use Javascript's setInterval to repeat a function that grabs the value of the page created in the previous step. Within this function, check to see what the value is. If the value is 1, don't do anything. If it's anything else, modify the window.location value to go to the next page.
esqew
  • 42,425
  • 27
  • 92
  • 132