在一个文本标签中输入电子邮件,当onblur时触发验证事件,到数据库检验是否已经存在该用户,其中dispear方法即为onblur事件触发的方法。已经验证服务器已经收到本地发送的数据并且验证成功,但是echo不回去。
js代码如下:
var myXmlHttpRequest;
function getXmlHttpObject(){
   var xmlHttpRequest;
   try{
     xmlHttpRequest=new XMLHttpRequest();
     
   }
catch(e){
   try{
      xmlHttpRequest=new ActiveXObject("Msxml2.HMLHTTP");
      
   }
   catch(e){
      xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
      
   }
 }
   return xmlHttpRequest;
 }
function isExist(email){

    myXmlHttpRequest=getXmlHttpObject();
    if(myXmlHttpRequest){
     alert("hello"+email);
    
    var url="/renren/register_legal_Process.php";
    var data="email="+email;
    myXmlHttpRequest.open("post",url,true);
    myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    // 指定回调函数
    
    
   myXmlHttpRequest。onreadystatechange=deal;
    myXmlHttpRequest.send(data);
    
    //alert(myXmlHttpRequest.readyState);
    }
}
function deal(){

alert('hello');
    if(myXmlHttpRequest.readyState==4){
    
     var isExist=myXmlHttpRequest.responseText;
    
     if(isExist=="exist"){
     $("focus1").style.display="none";
     $("wrong1").style.display="none";
     $("duihao").style.display="none";
     $('wrong5').style.display="block";
     }else if(isExist=="notexist"){
     $("focus1").style.display="none";
     $("wrong1").style.display="none";
     $("wrong5").style.display="none";
     $("duihao").style.display="block";     
     }
    
    
    } 
   
}
function $(id){
  return document.getElementById(id);
}  
function disappear(ref1,ref2,ref3,ref4){
    var app=$(ref1);
 var tmp1=$(ref2);
 var tmp2=$(ref3);
 var tmp3=$(ref4);
 var str=tmp1.value;
 var reg=/^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi;         if(!reg.test(str))
{
     tmp3.style.display="none";
 app.style.display="none";
 $("wrong5").style.display="none";
 tmp2.style.display="";
}
else
{   
/*
 * tmp2.style.display="none"; app.style.display="none";
 * tmp3.style.display="";
 */
//var email=$('email').value;
isExist(str);
                  }

 if(str=="")
 {
 app.style.display="none";
 tmp2.style.display="none";
 tmp3.style.display="none";
 }  
      
}
服务器端代码如下:
<?php
 header("Content-type:text/html;charset=utf-8;");
  header("Cache-Control:no-cache");
  require_once 'UserService.class.php';
    $email=$_POST['email'];
    //file_put_contents("log.txt",$email."\r\n",FILE_APPEND);
    $userService=new UserService();
    $isExist=$userService->isExist($email);
    if($isExist){
     file_put_contents("log.txt","exist"."\r\n",FILE_APPEND);
     echo "exist";  
    
    }else{
     file_put_contents("log.txt","notexist"."\r\n",FILE_APPEND);
     echo "notexist";
    
    }?>xmlhttprequestajax服务器