简体   繁体   中英

Refresh page with different datas without refreshing browser

Helloo, I have been making a project in with I have to refresh the data every 10 seconds, I already created a separate file to load one info and it worked, but my question is, I have 3 sensors data on my database, Will I have to create 3 different refresh files to load each data to my main page?

This is one of my data files:

<?php 

  session_start();

  include_once 'includes/dbh.inc.php';

  $id = $_SESSION['userId']; 

  $dBname = "infosensor";
  $conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBname);

  $sql = "SELECT * FROM `$id` ORDER BY id DESC LIMIT 1;";
  $result = mysqli_query($conn, $sql);
  $resultCheck = mysqli_num_rows($result);

  if($resultCheck > 0)
  {
    while ($row = mysqli_fetch_assoc($result))
    {
      $ss1 = intval($row['sensor1'] * ($p = pow(10, 2))) / $p;
      echo "".$ss1."A";
      $s1 = $row['sensor1'];
    }
  }
?>

this is my update file:

<script type="text/javascript" src="jquery-3.4.1.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function (){
        setInterval(function () {
          $('#p0-blocoCorrente').load('load.php')
        }, 5000);
      });
    </script>

this is how i diplay it:

<div class="blocoCorrente" id = "blocoCorrente">
      <!-- Imprimir os valore dos sensor 1 -->
      <div class="p0-blocoshow">Corrente 1:</div>
      <div class="p0-blocoCorrente" id ="p0-blocoCorrente"></div>
</div>

UPDATE:

 session_start();

  include_once 'includes/dbh.inc.php';

  $id = $_SESSION['userId']; 
  $sensor = isset($_POST['sensor']) ? "sensor" . intval($_POST['sensor']) : "sensor1";
  $dBname = "infosensor";
  $conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBname);

  $sql = "SELECT $sensor FROM `$id` ORDER BY id DESC LIMIT 1;";
  $result = mysqli_query($conn, $sql);
  $row = mysqli_fetch_assoc($result);
  if($row)
  {

    $s1 = $row[$sensor];
    $ss1 = intval($s1 * ($p = pow(10, 2))) / $p;
    echo $ss1;
  }

  $sensor1 = isset($_POST['sensor']) ? "sensor" . intval($_POST['sensor']) : "sensor2";

  $sql = "SELECT $sensor1 FROM `$id` ORDER BY id DESC LIMIT 1;";
  $result = mysqli_query($conn, $sql);
  $row = mysqli_fetch_assoc($result);
  if($row)
  {

    $s2 = $row[$sensor1];
    $ss2 = intval($s1 * ($p = pow(10, 2))) / $p;
    echo $ss2;
  }

Make the PHP script return all the sensors in a JSON object. Then you can display each of them in an appropriate DIV.

session_start();

include_once 'includes/dbh.inc.php';

$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;
        $sensors["sensor$i"] = $ss1 . "A";
    }
    echo json_encode($sensors);
} else {
    echo json_encode(null);
}

Then change the JavaScript to use $.getJSON() .

$(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);
        }
      });
    }, 5000);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM