Matheus
Matheus

Reputation: 83

Problem getting JSON variable from another file

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);
  }
?>

Upvotes: 1

Views: 52

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

In the PHP add soma to the array before json'ing it

<?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";
    }

    // add soma to the array
    $sensors['soma'] = $soma;
    echo json_encode($sensors);
} else {
    echo json_encode(null);
}
?>

Then in the javascript, get it out to whereever you want to place it

<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);

                $('#p3-blocoCorrente').text(sensors.soma);
            }
        });
    }, 5000);
});
</script>

Upvotes: 1

Related Questions