I'm trying to get an Object from my file called load.php, I'm trying to get the information from load.php every 5s. For some reason, I could make one part of it, but I can't seem to get the other variable.
<script type="text/javascript" src="jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){
setInterval(function () {
$.getJSON('load.php', function(sensors) {
if (sensors)
{
$('#p0-blocoCorrente').text(sensors.sensor1);
$('#p1-blocoCorrente').text(sensors.sensor2);
$('#p2-blocoCorrente').text(sensors.sensor3);
//Error here:
$('#p3-blocoCorrente').text(soma);
}
});
}, 5000);
});
</script>
This is my load.php file:
<?php
session_start();
include_once 'includes/dbh.inc.php';
$soma = 0;
$id = $_SESSION['userId'];
$dBname = "infosensor";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBname);
$sql = "SELECT sensor1, sensor2, sensor3 FROM `$id` ORDER BY id DESC LIMIT 1;";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$sensors = array();
if($row)
{
for ($i = 1; $i <= 3; $i++) {
$s1 = $row["sensor$i"];
$ss1 = intval($s1 * ($p = pow(10, 2))) / $p;
$soma += $row["sensor$i"];
$sensors["sensor$i"] = $ss1 . "A";
}
echo json_encode($sensors);
} else {
echo json_encode(null);
}
?>
$somafrom the php, nor where you would receive it in the ajax success. So when you say you "can't seem to get the other variable" do you mean you can't output the other variable or that it's not in javascript where, for some reason, you're expecting it to be?