javascript - 重复从 SQL 服务器更新 HTML 表

标签 javascript php mysql ajax

我有一个 HTML 页面,里面有一个表,我想每 2 秒用存储在 MySQL 数据库中的值更新它的值。

setInterval(updateSensorsTable, 2000);
setInterval(updatePower, 2000);

function showValue(value) {
  document.getElementById("range").innerHTML = value;
}

function TurnLedOn() {
  //TODO: need to set the led state and update the sql server
  document.getElementById("ledStatus").src = "/LedOn.png";
}

function TurnLedOff() {
  //TODO: need to set the led state and update the sql server
  document.getElementById("ledStatus").src = "/LedOff.png";
}

function AjaxCaller() {
  var xmlhttp = false;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

function callPage(url, div) {
  ajax = AjaxCaller();
  ajax.open("GET", url, true);
  ajax.onreadystatechange = function() {
    if (ajax.readyState == 4) {
      if (ajax.status == 200) {
        div.innerHTML = ajax.responseText;
      }
    }
  }
  ajax.send(null);
}

function updateSensorsTable() {
  for (i = 0; i <= 7; i++)
    callPage('/getVarFromDB.php?offset=' + i, document.getElementById(i));
}

function updatePower() {
  document.getElementById("powerValue").innerHTML = '200';
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0" />
  <title>SmartLight</title>
</head>

<body bgcolor="#E6E6FA" onload='updateSensorsTable()'>
  <br></br>
  <table class="sensorsTable" align="center">
    <thead>
      <tr>
        <th>Sensor</th>
        <th>Value</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td align="left">GPS</td>
        <td align="center" id="0">0</td>
      </tr>
      <tr>
        <td align="left">Temperature</td>
        <td align="center" id="1">0</td>
      </tr>
      <tr>
        <td align="left">Pressure</td>
        <td align="center" id="2">0</td>
      </tr>
      <tr>
        <td align="left">Light</td>
        <td align="center" id="3">0</td>
      </tr>
      <tr>
        <td align="left">Altitude</td>
        <td align="center" id="4">0</td>
      </tr>
      <tr>
        <td align="left">Accelerometer</td>
        <td align="center" id="5">0</td>
      </tr>
      <tr>
        <td align="left">Humidity</td>
        <td align="center" id="6">0</td>
      </tr>
      <tr>
        <td align="left">Camera</td>
        <td align="center" id="7">0</td>
      </tr>
    </tbody>
  </table>

  <br></br>

  <table class="ledTable" align="center">
    <tr>
      <td align="center">
        <input type="image" src="/TurnOn.png" id="turnOn" width="60" height="60" onclick='TurnLedOn()'>
      </td>
      <td align="center">
        <input type="image" src="/TurnOff.png" id="turnOff" width="60" height="60" onclick='TurnLedOff()'>
      </td>
      <td align="center">
        <img src="/LedOn.png" style="width:60px;height:60px" id="ledStatus">
      </td>
    </tr>
    <tr>
      <td align="center" id="ledOnButton">LED On</td>
      <td align="center" id="ledOffButton">LED Off</td>
      <td align="center">LED Status</td>
    </tr>
  </table>

  <div align="center">
    Brightness:
    <input type="range" name="brightness" min="0" max="100" value="0" step="1" onchange="showValue(this.value)" />
    <span id="range">0</span>
    <table align="center" class="power">
      <tr>
        <td align="center" id="powerValue">0</td>
        <td align="left">mW</td>
      </tr>
    </table>
    <div align="center">LED Power Meter</div>
  </div>
</body>
</html>

这是PHP代码:

<?php
include("connect_to_mysql.php");

$result = mysql_query("SELECT value FROM sens" );

echo mysql_result($result,$offset);

请帮助我以正确的方式做到这一点。 当我使用 for 循环时,这段代码不起作用。将此代码与直接分配一起使用,例如 callPage('/getVarFromDB.php?offset=' + 1, document.getElementById(1));正在工作

最佳答案

"I have an HTML page with a table in it which I want to update it's values every 2 seconds with the values stored in the MySQL database"

我错过了什么吗? JavaScript 在哪里输入(Das Blinkenlights 除外)?

是否有任何令人信服的理由不只使用 HTML 页面和 auto-refresh每 2 秒一次 <meta http-equiv="refresh" content="2000">

这就是 KISS 解决方案。

关于javascript - 重复从 SQL 服务器更新 HTML 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28429032/

相关文章:

PHP 比较正则表达式和字符串

PHP、AJAX、MYSQLI 和 IE9 未按预期输出

javascript - 在 Windows 10 中为 Electron 安装 node_usb_detection

javascript - 如何在 Angularjs 中验证 IpV6 地址

javascript - 是否可以使用 CommonJS 库?

php - 使用 "load data infile"时如何记录错误并将其写入日志文件?

javascript - IE8 JavaScript 弹出窗口 "Stackoverflow at line 3"

mysql - 从本地服务器连接到 MySQL 很慢

php - CakePHP 中的 $data findAll 查询检查第二个表中的值

sql - mysql 控制台中没有可用的先前输出文件