我要做个局部刷新这两个字段,比如1分钟刷新一次,数据从mysql数据库里调取的!
<td><?php echo $row['status']?></td>
<td><?php echo $row['time']?></td>
<td><?php echo $row['status']?></td>
<td><?php echo $row['time']?></td>
调试欢乐多
if(isset($_GET['refresh'])){
//数据库查询操作
//输出结果
echo "<td>{$row['status']}</td><td>{$row['time']}</td>";
}#页面js
<script>
var xmlHttp;
var timer = null;
var timerRunning = false;
var url; function createXMLHttpRequest() {
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
} function statusStop (){
if(timerRunning){
clearTimeout(timer);
}
timerRunning = true;
} /* ------------------- 间隔查询 ------------------- */
function autoDeep(){
createXMLHttpRequest();
url = "data.php?refresh="+Math.random(); //后端查询数据页面
method = "GET";
xmlHttp.open(method,url,true);
xmlHttp.onreadystatechange = show(); //执行回调函数,处理后端返回的结果
xmlHttp.send(null);
timerRunning = false;
timer = setTimeout('autoDeep()',5000); //此处为5秒执行一次
}
function show(){
if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200){
var text = xmlHttp.responseText;
document.getElementById("route").innerHTML = text; //这里将返回的数据text放入页面id为route的div中。
}else {
alert("response error code:"+xmlHttp.status);
}
}
} function statusBegin(){
statusStop();
autoDeep();
} statusBegin();</script>#前端页面
<div id="route">
<td><?php echo $row['status']?></td>
<td><?php echo $row['time']?></td>
</div>
#注意
url = "data.php?refresh="+Math.random(); //后端查询数据页面#js中的url指向你的后端php处理数据的页面,即,$rows的出处,所以;
#data.php
if(isset($_GET['refresh'])){ //这里接收url的参数refresh
//数据库查询操作,即你的$rows出处
//输出结果
echo "<td>{$row['status']}</td><td>{$row['time']}</td>";
}
if(isset($_GET['refresh'])){ //这里接收url的参数refresh
//数据库查询操作,即你的$rows出处
$sql = "select * from table";
$res = mysql_query($sql);
if(!$res)
die("SQL:{$sql}<br>Error:".mysql_error());
if(mysql_affected_rows() > 0){
while($rows=mysql_fetch_array($res,MYSQL_ASSOC)){
echo "<td>{$row['status']}</td><td>{$row['time']}</td>";
}
}else{
echo "查询失败".mysql_error();
}
}
#ajax会将该页面的输出内容返回前端页面