[PHP]XMLHTTP无刷新技术
时间:2005-11-21 来源:yejr
其实更多的是利用了javascript,跟php关系并不大,PEAR有AJAX模块,可是懒得去看,这个也挺简单的。
举个简单的例子,适用于IE和Firefox
test.html:
function loadXMLTreeDoc()
{
var url = "/test.php";
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
//req.setRequestHeader("text","text");
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
req.send(null);
// branch for IE/Windows ActiveX version
}
else if (window.ActiveXObject)
{
isIE = true;
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req)
{
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
//req.setRequestHeader("text","text");
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
req.send();
}
}
//refresh on time
setTimeout( "loadXMLTreeDoc()" , 500 );
}
// handle onreadystatechange event of req object
function processReqChange() {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.all.t.innerHTML=req.responseText;
req=null;
} else {
alert("Sorry,there was a problem retrieving the XML data, please check network!");
}
}
}
数据载入中
test.php:
在test.html中每0.5妙就自动去请求test.php,将返回的结果用JAVASCRIPT的方式写出来,实现了无刷新结果。
举个简单的例子,适用于IE和Firefox
test.html:
function loadXMLTreeDoc()
{
var url = "/test.php";
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
//req.setRequestHeader("text","text");
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
req.send(null);
// branch for IE/Windows ActiveX version
}
else if (window.ActiveXObject)
{
isIE = true;
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req)
{
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
//req.setRequestHeader("text","text");
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
req.send();
}
}
//refresh on time
setTimeout( "loadXMLTreeDoc()" , 500 );
}
// handle onreadystatechange event of req object
function processReqChange() {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.all.t.innerHTML=req.responseText;
req=null;
} else {
alert("Sorry,there was a problem retrieving the XML data, please check network!");
}
}
}
数据载入中
test.php:
在test.html中每0.5妙就自动去请求test.php,将返回的结果用JAVASCRIPT的方式写出来,实现了无刷新结果。
相关阅读 更多 +