2012-06-04 45 views
0

這裏是案件球員,我想檢查onblur事件的用戶名與ajax,這是檢查用戶名可用性在mysql數據庫的幫助。ajax檢查用戶名onblur

這裏是AJAX腳本=>

document.getElementById("r_username").onblur = function(){ 
     var http = false; 
     var error = document.getElementById("error_username"); 
     var numLetter = /^[a-zA-Z-0-9]+$/; 
     if (this.value==""){ 
      error.innerHTML = "Empty Field !!!"; 
      error.style.display = "inline"; 
     } else { 
      if (this.value.match(numLetter)){ 
       if (window.XMLHttpRequest){ 
        http = new XMLHttpRequest(); 
       } else { 
        http = new ActiveXObject("Microsoft.XMLHTTP"); 
       } 
       if (http){ 
        http.open("POST","./config/AjaxUsernameEmail.php",true); 
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
        http.onreadystatechange = function(){ 
         if (http.readyState==4 && http.status==200){ 

         } 
        }; 
        http.send("r_username=" + document.getElementById("r_username").value); 
       } 
       error.innerHTML = ""; 
       error.style.display = "none"; 
      } else { 
       error.innerHTML = "Invalid Number !!!"; 
       error.style.display = "inline"; 
      } 
     } 
    }; 

阿賈克斯成功合作和PHP文件也該腳本如下=>

class Checking{ 
private $con,$query,$flag; 
public function __construct($con,$query){ 
    $this->con = $con; 
    $this->query = $query; 
} 
public function func(){ 
    if (mysqli_connect_errno()==0){ 
     if ($result = mysqli_query($this->con,$this->query)){ 
      if ($data = mysqli_fetch_assoc($result)){ 
       return $this->flag = true; 
      } else { 
       return $this->flag = false; 
      } 
     } 
    } 
} 
} 

if (isset($_POST['r_username'])){ 
    $check = new Checking($connection,"SELECT username FROM users WHERE username='" . $_POST['r_username'] . "'"); 

} else { 
    header("Location: http://" . $mysql->host . "/index.php"); 
} 

一切工作正常,但這裏的問題,我想以某種方式連接這個文件,我的意思是我想在.js文件中知道當用戶名在數據庫中匹配,而不是時,因爲我想在.js文件中做更多的動作,但是我不能設置「標誌」 (變量會幫助我)。 任何想法?謝謝:)))

在更多的細節,.js文件是在registration.php文件中,以及如何看到球員.js文件正在調用AjaxUsernameEmail.php文件,所以我想做點什麼來知道什麼時候因爲我想在registration.php文件中做更多的動作(通知)匹配

回答

1

的代碼可以更有點像這樣:

$return = 'fail'; 

class Checking { 

    public function __construct($con, $query) 
    { 
     $this->con = $con; 
     $this->query = $query; 
     self::func() 
    } 

    public function func() 
    { 
     $result = 'ok'; 
     if (mysqli_connect_errno()==0){ 
      if ($result = mysqli_query($this->con,$this->query)){ 
       $result = mysqli_num_rows($result) > 0? 'user_exists' : 'user_doesnt_exist'; 
      } 
     } 
     return $result; 
    } 

} 

if($_POST['r_username']){ 
    $desired = mysqli_real_escape_string($_POST['r_username']); 
    $return = new Checking($connection,"SELECT username FROM users WHERE username='$desired'"); 
} 
echo $return; 

此外,你應該擔心逃跑用戶輸入,並且可能要看看jQuery你的ajax的東西。

在客戶端的檢查,應該是這樣的:

if (http.readyState==4 && http.status==200){ 
    switch (http.responseText){ 
     case 'fail': 
      //the username was not provided 
     break; 
     case 'user_exists': 
      //the username already exists 
     break; 
     case 'user_doesnt_exist': 
      //the username was not found on the database, continue 
     break; 

    } 
} 
1

對於阿賈克斯請求你不能return的值,但printecho它。嘗試

if ($data = mysqli_fetch_assoc($result)){ 
    echo $this->flag = true; exit; 
} else { 
    echo $this->flag = false; exit; 
} 

Evaluationg響應:

if (http.readyState == 4 && http.status == 200) { 
    switch (http.responseText) { 
    case 1: //user name taken, diplay error message 
    break; 
    case 0: //user name available, no action required 
    break; 

    } 
} 
+0

這不是主要的問題,也這是工作沒有回聲和/或打印。我只測試過AjaxUsernameEmail.php文件,所以=>在提到的文件腳本中我已經將POST替換爲GET方法,並且在瀏覽器URL中輸入了.php?r_username =(名稱與mysql數據庫匹配)並且具有調用echo $ check-> func();是被調用的,並且如果它匹配的話給我寫1,如果它不匹配則不寫。在這裏,我的問題是,我想以某種方式在.js文件中知道這個匹配問題以執行更多操作 – tnanoba