I'm trying to refresh data on my web page according to the data stored in the database, so every 2 seconds I call with an ajax request a php file. The called php script is this:
session_start();
.....Connection to the db.......
$prova = pg_query($connect, "SELECT * FROM maxdistance");
$prova2 = "";
while ($row = pg_fetch_row($prova)) {
$prova2 = $prova2.$row[0].$row[1].$row[2];
}
$_SESSION['prova'] = $prova2;
And this is the code in javascript:
var intervalId = window.setInterval(function(){
newPositions();
}, 2000);
function newPositions(){
$(document).ready(function(){
$.ajax({
type: "POST",
url: "realTimePosition.php",
success: function(msg){
provaaa = <?php echo ($_SESSION[prova]); ?>;
}
})
});
The problem is that, when I refresh the page the code run and in the variable provaaa is stored the value 20 (the actual value in the db) but if I modify the value in the db, the value of the variable is the same, why is this happening?
you cant mix JS and PHP like this
problem part
provaaa = <?php echo ($_SESSION[prova]); ?>;
solution
echo json_encode([ 'data' => $prova2]);
success: function(res){
provaaa = JSON.parse(res).data;
}