如何使用AJAX和PhP在specefic时间间隔后从postgresql数据库更新表

我正在使用这个问题中完成的技术。 如何使用ajax和php将数据从数据库表放入html表 。

我的任务目标是:在特定的时间间隔之后,在1分钟后更新来自postgresql数据库的表。 我想使用Ajax因为我不想要那个客户端referesh页面。 它应该自动完成。 在应用ajax之前,我的代码位于: estat hardware.php文件中

         

Estat Tags

<?php $con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n"); $query = "SELECT * FROM beacon"; $result = pg_query($con, $query) or die("Cannot execute query: $query\n"); $i = 0; echo ''; echo '"; echo ""; echo ""; echo ""; echo ""; echo ""; } pg_free_result($result); echo '
' . "TAG" . ''; echo '' . "BATERIA" . ''; echo '' . " VIST ÚLTIMA COP " . ''; echo '' . "ESTAT" . ''; $i = 0; while ($row = pg_fetch_array($result)){ $estat="TODO"; echo "
".$row[1]."".$row[3]."".$row[2]."".$estat."
'; pg_close($con); ?>

我想使用AJAX更新同一个表。 所以,我研究了ajax并编写了调用服务器函数的机制,服务器将从数据库中获取数据并使用json重新生成它。服务器文件是: estat_hardware_server.php

 $row['mac'], 'ts' =>$row['ts'], 'battery'=>$row['battery'] ); } echo json_encode($data); pg_free_result($result); pg_close($con); } ?> 

在客户端,我有以下代码,应该从ajax更新。

         

Estat Tags

<?php echo ''; echo '
' . "TAG" . ''; echo '' . "BATERIA" . ''; echo '' . " VIST ÚLTIMA COP " . ''; echo '' . "ESTAT" . ''; echo '
'; ?>

我在客户端测试的ajax代码是否正确接收来自json的数据:

    

var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myObj = JSON.parse(this.responseText); for (i = 0; i < myObj.length; i++) { // I need to update the table here, after time interval 1 minute. } } }; xmlhttp.open("GET", "pages/estat_hardware_server.php", true); xmlhttp.send();

将ajax方法放入函数中,每1分钟调用一次该函数。