1

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);
  }
?>
5
  • Can't see where you output $soma from 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? Commented Oct 31, 2019 at 11:40
  • That's the problem, I should put the $soma in the function? Commented Oct 31, 2019 at 11:41
  • I want to display it, but I just don't know how to get it into the script Commented Oct 31, 2019 at 11:42
  • What could I do than? Commented Oct 31, 2019 at 11:42
  • You mean echo json_encode($soma)? Commented Oct 31, 2019 at 11:42

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

Basically yes, its really quite simple once you get the first couple under your belt isn't it
This comes from javascript right? It's because I actually have to get borrow some time to learn decent javascript, I'm really noob
No - this has nothing to do with javascript / ajax. That part you had fine. It's the server-side php that provides the information that your javascript has access to, if you don't include it in the output, you can't see it in the javascript. This answer shows you how to change the php to include it.
@freedomn-m I see where you are coming from there, but the server response IS an integral part of AJAX process
Well, yes, you need a server response, but it's not part of Asynchronous Javascript. Better to keep the separation of concerns, especially when learning. Javascript makes an ajax request; the server provides a response. When you change the server side, you're not changing the javascri[t/ajax, which is what was being asked with "this comes from javascript, right?". I may have been unclear when saying "nothing to do with javascript" - the change to the server-side/php is nothing to do with javascript. It's important to keep them separate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.