ajax的应用

ajax的创建 ajax.js

//创建ajax对象
function c_xmlhttp(){
  var xmlhttp=null;

   if(window.XMLHttpRequest){
       
           xmlhttp= new XMLHttpRequest;  
        //如果请求的不是xml 则将它转为xml
           if(xmlhttp.overrideMime Type){
                xmlhttp.overrideMime Type("text/html")
               }
   }
  else if(window.ActiveXObiect){

          try{
              
               xmlhttp=new ActiveXobject(Wsxml2.XMLHTTP);
        }catch(e){

                try{
                           
                  xmlhttp=new ActiveXObject(Microsoft.XNLHTTP);  
                 }catch(e){}
            }
  }
return xmlhttp;
}
xmlhttp=c_xmlhttp();

function getdata(id){

      xmlhttp.open('GET','index.php?id='+id,true);
      xmlhttp.onreadystatechange=handle;
      xmlhttp.send();
}

function handle(){
     if(xmlhttp.readystate==4){
           if(xmlhttp.status==200){
                    var text=xmlhttp.responseText;
                    document.getElementById('content').innerHTML=text;
             }

    }

}

inddex.html

<html>
      <head>
                <title>测试ajax</title>
<script  type="text/javascript" src="ajax.js"></script>
       </head>
 <body>
   名称:   <input type="text" name='name' value="" onclick="getdate(this.value)">
 <div id="content"></div>
 </body>
</html>

index.php

<?php

if(isset($_GET['id'])){
 $id=$_GET['id'];
$mysqli=new mysqli('loclhost','root','my123','myuser') or die("链接失败!");
$query=$mysqli->query("select *from user where name=".$id);
  if(is_array(mysqli_fetch_row($query))){
        echo "该用户已存在";
  }else
          echo "可以使用";
}

?>
原文地址:https://www.cnblogs.com/bugs/p/2765393.html