使用Ajax或Jquery每5s更新一次PHP内容。

问题描述 投票:0回答:1

我想每5秒更新一次我的php页面,这样新的数据就可以进入到一个.json文件中,所以基本上它要做的是,从数据库中获取数据,然后用这些数据创建一个.json文件,我需要这个php文件每5秒更新一次,所以.json文件也在更新。代码(JsonfileAPI.php)。

<!DOCTYPE html>
<html lang="pt-br">
<head>
</head>
<body>
<?php
    function get_data()
    {
        $servername = "nps";
        $dBUsername = "nps";
        $dBPassword = "nps";
        $dBname = "nps";

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

        if ($conn->connect_error){
        die ("Connection failed". $conn->connect_error);
        }

        $sql = "SELECT * FROM dados;";
        $result = mysqli_query($conn, $sql);
        $json_array = array();
        while($row = mysqli_fetch_assoc($result))
        {
            $json_array[] = array(
                'AutoIncrement'         =>       $row["AutoIncrement"],
                'Aparelho'         =>       $row["aparelho"],
                'Status'         =>       $row["Status"],
            );
        }
        return json_encode($json_array);
    }
    $file_name = 'dadosjson' . '.json';

    if (file_put_contents($file_name, get_data()))
    {
        echo $file_name. ' file created';
    }
    else 
    {
        echo 'There is some error';
    }
?>
<script>
$(document).ready(function () {
setInterval(function(){
    $.ajax({
        url: "JsonfileAPI.php",
        type: 'post',
        dataType: 'json',
        success: function(response) {
            $('.time').html(response);
        }
    });
    }, 5000);
});
</script>

php jquery ajax refresh
1个回答
1
投票

你可以在你的应用程序中使用这段代码,我建议使用单独的文件,如index.php(用于你的html代码)和action.php用于PHP脚本。并且不需要.json文件。

试试这个,在index.php中的html部分和javascript部分像这样

 $(document).ready(function () {
     setInterval(function(){
            $.ajax({
                url: "action.php",
                type: 'post',
              //  dataType: 'json',
                success: function(response) {
                   console.log(response);
                  $('.time').html(response);
              }
            });
        }, 5000);
      });
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>

    <div class="time">
    </div>
    </body>
</html>

那么你的action.php文件应该是这样的,

  <?php
    $localhost = "nps";
    $username = "nps";
    $password = "nps";
    $dbname = "nps";

    // db connection
    $connect = new mysqli($localhost, $username, $password, $dbname);
    // check connection
    if ($connect->connect_error) {
      die("Connection Failed : " . $connect->connect_error);
    } else {
     //  echo "Successfully connected".$dbname;
    }

    $sql = "SELECT * FROM dados";
    $result = $connect->query($sql);

    $output = array('data' => array());

    if ($result->num_rows > 0) {
      while ($row = $result->fetch_array()) {
      $output['data'][] = array(
      $row[0],
      $row[1],
      $row[2]
     );
    }
   }
   $connect->close();

   echo json_encode($output);

我已经检查过了,工作正常。觉得会有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.