Ajax Live数据库检查

时间:2010-11-09 17:08:39

标签: php jquery ajax xmlhttprequest

我正在制作一个注册表,用于检查所输入的手机号码是否正在使用,就像在twitter的用户名检查中一样。我的代码看起来很完美,但我一直得到'检查号码可用性'就像ajax不是在发布我的请求。请帮忙 :-) 这是相关的ajax代码位

<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>

2 个答案:

答案 0 :(得分: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 :(得分:-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>