0

I'm trying to update a value from the database in real-time using Javascript and PHP. Though the value keeps updating in the database, it doesn't refresh on screen unless I manually refresh the page. Any pointers?

div class="panel-body" id = "tweetCounter">
<script>
//Javascript function that calls the PHP function
function tweetCounter()
{
var getTweetCount = <?php updateTweetCount();?>;
document.getElementById('tweetCounter').innerHTML = getTweetCount;
}
tweetCounter();

//Call function every 30 seconds
setInterval(function(){
tweetCounter();
}, 30000);
</script>

<!--PHP function to fetch details from DB -->
<?php
function updateTweetCount()
{
include('dbconnect.php');
$sql = "SELECT COUNT(tweetId) as tweetcount from brexittweets";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["tweetcount"];
}
} else {
echo "0 results";
}
}
?>

</div>

0 Answers0