在AJAX帮助中刷新 [英] Refresh in AJAX Help
问题描述
我希望我的网站上有一个框架,如果用户有新消息则显示一张图片,如果是dsnt则显示另一张不同的图片。在我每隔10秒使用html自动刷新页面之前。
这对用户来说很烦人。因为用户可以注意到刷新和用于闪烁的图像。
i发现AJAX可以为我提供更好的刷新,因此我尝试使用它。
>
但是帧dsnt会自动刷新,但是当我手动刷新时它会显示正确的信息。
[PHP]<?php
session_start();
包括includes / db_connect.php;
包括includes / functions.php;
logincheck();
$ username = $ _ SESSION [''username''];
$ query = mysql_query(" SELECT * FROM users WHERE username =''$ username''");
$ fetch = mysql_fetch_object($ query);
$ inbox_msg = mysql_num_rows(mysql_query(" SELECT) * FROM`inbox` WHERE`read` =''0''和`to` =''$ username''"));
$ check = mysql_query(" SELECT`to` ,`read` FROM`inbox` WHERE`read` =''0''和`to` =''$ username''");
? >
<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN">
< html>
< head>
< script language =" javascript">
function createRequestObject( ){$ / $
if(window.XMLHttpRequest){
// Firefox,Safari, Opera ...
req = new XMLHttpRequest();
} else if(window。 ActiveXObject){
// Internet Explorer 5+
req = new ActiveXObject(" Microsoft.XMLHTTP");
} else {\\ n br />
//创建对象时出错,
//就像使用旧浏览器一样。
alert(" Your Browser)不支持此脚本 - 请尽快升级您的浏览器);
}
返回请求;
} $ / $
//创建XMLHttpRequest对象
var http = createRequestObject();
函数sendRequest(页面)$
//为请求打开PHP脚本
http.open(''get'',page);
http.onreadystatechange = handleResponse;
http.send(null);
}
函数handleResponse() {
if(http.readyState == 4&& http.status == 200){
//从PHP脚本返回的文本
var response = http.responseText;
if(response){
// UPDATE ajaxTest content
document.getElementById(" msgstatus")。innerHTML = response;
}
}
}
函数repeatloop()
{
sendRequest(''box.php''); // replace" inbox-status.php"用你的php页面'url
setTimeout(" repeatloop()",3000);
}
window.onload = function(){
repeatloop();
}
< / script>
< title> The Legendary Mafia< / title>
< meta http-equiv =" Content-Type"含量=" text / html的; charset = iso-8859-1">
< style type =" text / css">
<! -
body {
background-image:url(images / banner / gradient.gif);
background-repeat:repeat-x;
}
- >
< / style>< / head>
< body>
< table width =" 100%"边界=" 0"对齐= QUOT;中心" CELLPADDING = QUOT; 0" cellspacing =" 0">
< tr>
< td width =" 100%"高度= QUOT; 70"背景= QUOT;" scope =" col">
< div align =" center">< p>
<?php
$ inbox = mysql_num_rows($ check);
if($ inbox> 0){
echo"< a href = inbox.php target = middle>< img border = 0 src = images / unread1.gif align = center width = 16 height = 11>< / a>英寸;
}其他{
echo"< img src = images / read.gif align = center width = 16 height = 11>" ;;
}
?>
< span id =" msgstatus">< / span>< / p>
< / div>< / td>
< / tr>
< / table>
< / body>
< / html> [/ PHP]
username =
< blockquote> _SESSION [''username''];
query = mysql_query(" SELECT * FROM users WHERE username =''
Hi,
i want a frame on my website to display one pic if the user has new messages and another different pic if it dsnt. Before i used html to auto refresh page every 10 secs.
that was annoying for the user. as the user could notice the refresh and the images used to flicker.
i found out that AJAX could perfrom the refresh for me better hence i tried using that.
but the frame dsnt refresh automatically, but it does display correct info when i refresh manually.
[PHP]<?php
session_start();
include "includes/db_connect.php";
include"includes/functions.php";
logincheck();
$username=$_SESSION[''username''];
$query=mysql_query("SELECT * FROM users WHERE username=''$username''");
$fetch=mysql_fetch_object($query);
$inbox_msg=mysql_num_rows(mysql_query("SELECT * FROM `inbox` WHERE `read`=''0'' AND `to`=''$username''"));
$check = mysql_query("SELECT `to`,`read` FROM `inbox` WHERE `read`=''0'' AND `to`=''$username''");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript">
function createRequestObject() {
var req;
if(window.XMLHttpRequest){
// Firefox, Safari, Opera...
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
// Internet Explorer 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
// There is an error creating the object,
// just as an old browser is being used.
alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP");
}
return req;
}
// Make the XMLHttpRequest object
var http = createRequestObject();
function sendRequest(page) {
// Open PHP script for requests
http.open(''get'', page);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if(http.readyState == 4 && http.status == 200){
// Text returned FROM the PHP script
var response = http.responseText;
if(response) {
// UPDATE ajaxTest content
document.getElementById("msgstatus").innerHTML = response;
}
}
}
function repeatloop()
{
sendRequest(''box.php''); // replace "inbox-status.php" with your php page''s url
setTimeout("repeatloop()", 3000);
}
window.onload=function() {
repeatloop();
}
</script>
<title>The Legendary Mafia</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
background-image: url(images/banner/gradient.gif);
background-repeat: repeat-x;
}
-->
</style></head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" height="70" background="" scope="col">
<div align="center"><p>
<?php
$inbox=mysql_num_rows($check);
if ($inbox > 0){
echo "<a href=inbox.php target=middle><img border=0 src=images/unread1.gif align=center width=16 height=11></a> ";
}else{
echo "<img src=images/read.gif align=center width=16 height=11>";
}
?>
<span id="msgstatus"></span></p>
</div></td>
</tr>
</table>
</body>
</html>[/PHP]
username=
_SESSION[''username''];
query=mysql_query("SELECT * FROM users WHERE username=''
这篇关于在AJAX帮助中刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!