自己写的一个无刷新,消息提示功能
html页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>消息提示</title><script src="jquery-1.7.1.min.js"></script><script>//var i=0;function messagetishi($i){ var xhr; //定义一个ajax的对象 var text="localhost"; //表 单中国的的值,要通过url参数进行一步传送 var url="chuli.php?loc="+"localhost"+"&user="+"root"+"&pw="+""+"&i="+$i; //异步传送内容 xhr=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");//定义一个浏览器的通用对象 xhr.open("POST",url,true);//打开提交地址及其提交方法,和异步 xhr.send(null);//发送请求 xhr.onreadystatechange=function() //监听 {if(xhr.readyState==4) //事件追踪,看执行到哪了 { if(xhr.responseText != 0){ //i++; // alert(xhr.responseText); if($i==1){ document.getElementById("messages").innerHTML="<font color='green'>"+xhr.responseText+"</font><br />"+"<a href='javascript:void(0)' onclick='location.reload()'>返回首页</a>"; document.getElementById("message").innerHTML="<font color='red'>以下是您的消息!</font>"; } else { document.getElementById("message").innerHTML="<font color='red'>您有"+xhr.responseText+"未读消息!</font>";} } } }}function sx(){setInterval("messagetishi()",500);}</script></head><body onload="sx()"><span id="message" style="color:#009966; background-color:#FFCC99; cursor:pointer" onclick="messagetishi(1)">您目前没有消息</span><div id="messages" style="background-color:#FFCC99"></div></body> </html>
处理页面chuli.php
<?php $i=0; $loc=$_REQUEST['loc']; $user=$_REQUEST['user']; $pw=$_REQUEST['pw']; mysql_connect("$loc","$user","$pw") or die("链接有问题"); mysql_select_db("test") or die("连接数据库失败"); $sql="select *from a where status=1;"; $res=mysql_query($sql); $str=""; while($rows=mysql_fetch_assoc($res)){ $str.=$rows['name']."---<hr />"; $i++; } if($_REQUEST['i']==1){ $sql="update a set status=0 where status=1;"; $rr=mysql_query($sql); if($rr){echo $str;} }else{ if($i != 0){ echo $i; } else{ echo 0; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。