2015-02-10 22 views
1

我有一个HTML页面,里面有一个表格,我想每隔2秒更新一次数据值,并将值存储在MySQL数据库中。重复从SQL服务器更新HTML表格

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循环时,此代码不起作用。使用此代码直接分配新建分配FY如callPage('/getVarFromDB.php?offset=' + 1, document.getElementById(1)); is working

+2

我不想成为那个人,但不要使用'mysql'来代替'mysqli'。没有人可以真正回答这个问题**没有使用mysqli的权利** – Loko 2015-02-10 10:21:31

+0

您有效地在彼此之后激发7个xhr的请求,但将它们中的每一个存储在名为'ajax'的全局变量中。将全局更改为本地:'var ajax = AjaxCaller()',这应该起作用。但是,为什么使用七个独立的呼叫,将所有偏移量作为JSON发送并返回一个呼叫。 – Mouser 2015-02-10 10:29:02

+1

Mouser:你说得对。按照你的建议,它工作。调用7个请求的原因非常简单 - 我是新手,不知道我在做什么..我在C中为microChips定位,所以这项任务对我来说非常困难:) – bardalas 2015-02-10 11:21:40

回答

0

将全局更改为本地:var ajax = AjaxCaller(),这应该起作用。

0

“我有一个HTML页面中有一个表 我想更新它每2秒值与存储在MySQL数据库中的 值”

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

是否有任何令人信服的理由不仅仅是使用HTML页面,并且每2秒钟使用<meta http-equiv="refresh" content="2000">就可以使用auto-refresh

这就是KISS解决方案。

+0

嗨Mawg。不使用自动刷新的原因是因为它会刷新整个页面并重新加载所有图像等。你可以在我的代码中看到我已经添加了setInterval(updateSensorsTable,2000);这每2秒运行一次函数updateSensorTable。但请注意,updateSensorTable并不是非常有效,因为每次调用服务器时都会向服务器查询8次,并且每次都调用一个值。更新表中值的正确方法应该是在每次调用函数时查询1次并获取数组。然后使用JSON命令将其传递给JavaScript。 – bardalas 2015-02-10 15:01:40

+0

我明白了。然后抱歉回答。也许你可以在问题中澄清一下。祝你好运! – Mawg 2015-02-10 15:14:12

+0

仍然挣扎在如何从php传递数组到js – bardalas 2015-02-10 15:25:59