Reputation: 1173
I am trying to get my page to refresh if a field in the database changes.
I can get the JS to check every half second (speed for debugging) but it does not change value without refreshing the page, what should I be researching for this?
SetInterval(function() {
$.get('./include/update.php', function(data) {
var d = "<?php echo $pageNumFinal; ?>";
document.getElementById("test").innerHTML = d;
if(d !== data) {
document.getElementById("test1").innerHTML = data;
location.reload();
}
});
}, 500);
update.php
$stmt = $conn->prepare("SELECT pagenum FROM display WHERE id=1");
$stmt->execute();
$pageNumCount=$stmt->fetch(PDO::FETCH_ASSOC);
$pageNumFinal = $pageNumCount['pagenum'];
Upvotes: 0
Views: 96
Reputation: 3968
Edit: Updated my answer following our conversation in chat.
You are passing an argument data to the anonymous function, but are also declaring a variable called data and comparing them. Change the name of one of them at least.
setInterval(function() {
$.get('./include/update.php', function(data) {
var d = "<?php echo $pageNumFinal; ?>";
document.getElementById("test").innerHTML = data;
if (d !== data) {
location.reload();
}
});
}, 500);
Also, where you set the variable (d
) you are trying to set it to $pageNumFinal
which is a variable you set in update.php.
Make sure that in your file that contains this script you are getting the current page number and storing it in that variable which will get printed at run time and stored into d
.
Upvotes: 2