Home > php教程 > php手册 > php ajax 实例与ajax 教程

php ajax 实例与ajax 教程

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 09:57:40
Original
1114 people have browsed it

php ajax 实例与ajax 教程 1创建XMLHttpRequest对象的JavaScript程序。 2 发出异步请求的JavaScript程序。 3 处理服务器响应的JavaScript程序。

php教程 ajax 实例与ajax 教程
   1创建xmlhttprequest对象的网页特效程序。
  2   发出异步请求的javascript程序。
  3   处理服务器响应的javascript程序。

*/

 //1创建xmlhttprequest对象的javascript程序。

 function getxmlhttprequest()
{
    var xmlhttp=null;
    try
    {
        xmlhttp = new xmlhttprequest();                      //对于firefox等浏览器
    }
    catch(e)
    {
        try
        {
            xmlhttp = new activexobject("msxml2.xmlhttp");   //对于ie浏览器
        }
        catch (e)
        {
            try
            {
                xmlhttp = new activexobject("microsoft.xmlhttp");
            }
            catch(e)
            {
                xmlhttp = false;
            }           
        }
    }

return xmlhttp;
}

 


  //2   发出异步请求的javascript程序。
  
 

function sendrequest()
{
    //获取页面表单的文本框name的值
    var user_name = document.getelementbyid("name").value;

    if((user_name == null) || (user_name == ""))
        return;
   
    xmlhttp = getxmlhttprequest();
    if(xmlhttp == null)
    {
        alert("浏览器不支持xmlhttprequest!");
        return;
    }

    var url = "getusername.php";               //构建请求的url地址
    url = url + "?name=" + user_name;
   
    xmlhttp.open("get", url, true);            //使用get方法打开一个到url的连接,为发出请求做准备
   
    //设置一个函数,当服务器处理完请求后调用,该函数名为updatepage
    xmlhttp.onreadystatechange = updatepage;
    xmlhttp.send(null);                        //发送请求
}
 

// 3   处理服务器响应的javascript程序。
 

function updatepage()
{
    if(xmlhttp.readystate == 4)
    {
        var response = xmlhttp.responsetext;
        document.getelementbyid("userinfo").value = response;
    }
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template