程序每5秒刷新一次$pat_no
的值,我想知道的是,如何在查询完成后更改正在刷新的div的css类属性。在比较这些值之后,如果在数据库中更改了值,则类将相应地更改(如更改背景颜色),以便很容易注意到更改后的值,并在5秒后恢复到旧类。
这是代码
<?php
include 'connect.inc.php';
$query = "SELECT patno, compare, cubeno FROM tblcube WHERE cubeid = '1'";
if($query_run = mysql_query($query)){
while($row = @mysql_fetch_assoc($query_run)){
$com = $row['compare'];
$pat_no = $row['patno'];
$cube_no = $row['cubeno'];
if($com == $pat_no){
//change css class of <th>
echo "<th>".$pat_no."</th>";
}
else if ($com != $pat_no){
//change css class of <th>
echo"<th>".$pat_no."</th>";
$query2 = "UPDATE tblcube set compare = '$pat_no'where cubeid = '1'";
if($query_run= mysql_query($query2)){
}
}
}
}
&GT;
这是JS代码
function AJAX(){
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
return xmlHttp;
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
return xmlHttp;
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}
}
此外,这是index.php
的部分<head><script src="ajax1.js"></script>
<script type = "text/Javascript">
refreshdiv_c1v();</script>
<body>
//cubicle1
$query = "SELECT patno, cubeno FROM tblcube where cubeid = '1'";
if($query_run = mysql_query($query)){
while($row = mysql_fetch_assoc($query_run)){
$pat_no = $row['patno'];
$cube_no = $row['cubeno'];
echo " <tr class = 'rows' id = 'cub1'>
<th class = 'name'>".$cube_no."</th>
<th class = 'num'><div id = 'c1v'>".$pat_no."</div></th>
</tr>";
}}
</body>
答案 0 :(得分:0)
我的猜测是你的php文件中会出现这样的情况......
if($com==$pat_no){
return json_encode(array("msg"=>true,"data"=>$pat_no));
} else {
$query2 = "UPDATE tblcube set compare = '$pat_no'where cubeid = '1'";
if($query_run= mysql_query($query2)){
return json_encode(array("msg"=>false,"data"=>$pat_no));
}
}
在你的jquery中有类似的东西...
function checkPat(){
$.getJSON('yoururl.php',function(data){
if(data.msg==true){
// Update your class here
$('.theTRthatchanged').addClass('yourClass');
//Remove after 5 seconds
setTimeout(function(){
$('.theTRthatchanged').removeClass('yourClass');},5000);
} else if(data.msg==false){
//Do nothing I guess.
}
});
}
//Set the checker to fire every 2 seconds or whatever interval seems best.
setInterval(checkPat,2000);
不完全是这样,但这是它如何工作的主要思想。你如何选择改变的tr取决于你,可能有点棘手。您可能必须附加data-id属性或类似的东西,并将其与json一起发回。 但是你明白了。