2010-11-09 41 views
1

我正在製作一個註冊表,它將檢查輸入的手機號碼是否正在使用,就像在twitter的用戶名檢查中一樣。我的代碼看起來很完美,但我一直在'檢查號碼可用性',就像ajax沒有發送我的請求。幫助請:-) 這裏是有關Ajax代碼位Ajax Live數據庫檢查

<script type="text/javascript"> 
$(document).ready(function()//When the dom is ready 
{ 
$("#cellphone_number").change(function() 
{ //if theres a change in the username textbox 

var phonenumber = $("#cellphone_number").val();//Get the value in the username textbox 
if(phonenumber.length == 13)//if the lenght equal to 13 characters 
{ 
$("#availability_status").html('<align="absmiddle"><font color="#00FF33">Checking Number availability...</font>'); 
//Add a loading image in the span id="availability_status" 

$.ajax({ //Make the Ajax Request 
    type: "POST", 
    url: "../Functions/ajax_check_number.php", //file name 
    data: ("number="+phonenumber), //data 
    success: function(server_response) 
    {  
    $("#availability_status").ajaxComplete(function(event, request){ 

    if(server_response == '0')//if ajax_check_username.php return value "0" 
    { 
    $("#availability_status").html('<align="absmiddle"> <font color="#00FF33">Number is Available </font> '); 
    //add this image to the span with id "availability_status" 
    } 
    else if(server_response == '1')//if it returns "1" 
    { 
    $("#availability_status").html('<align="absmiddle"> <font color="#FF0000">Number already in use</font>'); 
    } 

    }); 
    } 

    }); 

} 
else 
{ 

$("#availability_status").html('<font color="#FF0000">Number too short</font>'); 
//if in case the username is less than or equal 3 characters only 
} 

return false; 
}); 

}); 
</script> 
+0

請格式化代碼正確 – cambraca 2010-11-09 17:37:35

+0

的代碼是所有靠不住的......我會修復它,但我沒有權限... – 2010-11-09 17:37:48

+1

您是否嘗試過使用Firebug,看看你的請求被貼? – 2010-11-09 18:24:31

回答

-1

現在代碼編輯至少到一定的水平,甚至是在Ajax和JavaScript像我這樣的初學者也能理解。這完美地完成了這項工作。添加一些GIF圖像以對客戶端用戶進行「可視化」響應,特別是在檢查數據庫時。

<script type="text/javascript"> 
$(document).ready(function()//When the dom is ready 
$(document).ready(function()//When the dom is ready 
{ 
$("#cellphone_number").change(function() 

{ //if there's a change in the cellphone_number textbox 
var phonenumber = $("#cellphone_number").val();//Get the value in the username textbox 
if(phonenumber.length == 13)//if the length is equal to 13 characters 
{ 
$("#availability_status").html('< align="absmiddle" >&nbsp;<font 
color="#00FF33">Checking Number availability...</font>'); 
//Add a loading image in the span id="availability_status" 
$.ajax({ //Make the Ajax Request 
type: "POST", 
url: "../Functions/ajax_check_number.php", //file name 
data: {number:$("#cellphone_number").val()},//data  
dataType: 'json', 
success: function(server_response) 
{  
$("#availability_status").ajaxComplete(function(event, request) 
{  
if(server_response == '0')//if ajax_check_number.php return value "0" 
{ 
$("#availability_status").html('<align="absmiddle"> <font color="#00FF33">Number is Available </font> '); 
//add this image to the span with id "availability_status" 
} 
else if(server_response == '1')//if it returns "1" 
{ 
$("#availability_status").html('<align="absmiddle"> <font color="#FF0000">Number already in use</font>'); 
} 
    }); 
} 
}); 
} 
else 
{ 

$("#availability_status").html('<font color="#FF0000">Number too short</font>'); 
//if in case the number is less than 13 characters only 
} 
return false; 
}); 
}); 
</script> 
0
<script type="text/javascript"> 
$(document).ready(function() 
{ 
    $("#cellphone_number").change(function() 
    { 
     var phonenumber = $("#cellphone_number").val(); 
     if(phonenumber.length == 13) 
     { 
      $("#availability_status").html('<align="absmiddle"><font color="#00FF33">Checking Number availability...</font>'); 
      $.ajax(
      { 
       type: "POST", 
       url: "../Functions/ajax_check_number.php", 
       data: {number: phonenumber}, 
       success: function(server_response) 
       { 
        if(server_response == '0') 
        { 
         $("#availability_status").html('<align="absmiddle"> <font color="#00FF33">Number is Available </font> '); 
        } 
        else if(server_response == '1') 
        { 
         $("#availability_status").html('<align="absmiddle"> <font color="#FF0000">Number already in use</font>'); 
        } 
       } 
      }); 
     } 
      else 
      { 

     $("#availability_status").html('<font color="#FF0000">Number too short</font>'); 
      } 
      }); 

}); 
</script> 
+1

你好,歡迎來到Stack Overflow。當您發佈代碼時還會發布有關它正在做什麼的解釋,以及它爲什麼可能解決該問題,這很有幫助。此外,這個問題是一個相當古老的問題,並被標記爲完整。你的回答可能會在更新的問題上得到更多的關注,你可以在主頁上查看。 – Ktash 2012-01-24 20:19:00