免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1558 | 回复: 0
打印 上一主题 下一主题

[PHP]XMLHTTP无刷新技术 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-21 11:32 |只看该作者 |倒序浏览
其实更多的是利用了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的方式写出来,实现了无刷新结果。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/82/showart_57928.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP