【问题标题】:Ajax Live Database checkAjax 实时数据库检查
【发布时间】:2011-05-07 09:21:08
【问题描述】:

我正在制作一个注册表,它将检查输入的手机号码是否正在使用,就像在 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>

【问题讨论】:

  • 请正确格式化代码
  • 代码很不稳定...我会修复它,但我没有权限...
  • 您是否尝试过使用 Firebug 来查看您的请求是否被发布?
  • 大家好,感谢您的努力。设法解决它。您需要做的就是在定义 url、数据、类型和成功的 AJAX 部分中添加 dataType = 'json'。我的代码很不稳定,但有人清理了它(我是新人)。谢谢。现在我不知道为什么它不能识别国际电话号码格式,例如 +254 ..... 必须是加号。希望能解决它或有人帮助我。再次感谢。

标签: php jquery ajax xmlhttprequest


【解决方案1】:
<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> 

【讨论】:

  • 您好,欢迎来到 Stack Overflow。当您发布代码以解释它在做什么以及为什么它可以解决问题时,它会很有帮助。此外,这个问题是一个相当古老的问题,已被标记为完成。您的回答可能会在较新的问题上得到更多关注,您可以在主页上查看。
【解决方案2】:

现在代码至少被编辑到即使像我这样的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 2017-10-06
    相关资源
    最近更新 更多