Ajax Post没有返回正确的值

时间:2016-07-26 09:38:58

标签: php jquery ajax post

当用户在表单字段中填入数字时,我正在尝试验证数据库中是否存在电话号码。我正在使用Ajax进行发布和返回确认。

除了从外部文件返回值时出现错误,一切似乎都很好。

我无法确定确切的错误原因

代码如下

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {  

// check if input data is number

        $('#phone').keyup(function(){isNumber(this);});

 //Check the min chars for phone  
        var min_chars = 10;  
        //result texts  
        var characters_error = 'Has to be 10 digits only';  
        var checking_html = 'Checking...';  
        //when button is clicked  
        $('#phone').keyup(function(){  
            //run the character number check  
            if($('#phone').val().length != min_chars){  
                //if it's bellow the minimum show characters_error text '  
                $('#phone_availability_result').html(characters_error);  
            }else{  
                //else show the checking_text and run the function to check  
                $('#phone_availability_result').html(checking_html);  
                check_availabilityp();  
            }  
        });  
});      

//function to check phone availability  
function check_availabilityp(){  
        //get the phone  
        var phone = $('#phone').val();  
        //use ajax to run the check  
        $.post("check_phone.php", { phone: phone },  

            function(resultp){  
                //if the result is 1  
                if(resultp == 1){  
                    //show that the phone is available  
                    $('#phone_availability_result').html(phone + ' is Available');  
                }else if (resultp == 0){  
                    //show that the phone is NOT available  
                    $('#phone_availability_result').html(phone + ' is not Available');  
                }else{//show that the phone is NOT available  
                    $('#phone_availability_result').html('Something Wrong');}  
            });

        };  

//function to check whether input is number

 function isNumber(field) {
    var re = /^[0-9]*$/;
    if (!re.test(field.value)) {
        alert('Must be all numeric charcters. Non numerics will be removed   from field!');
        field.value = field.value.replace(/[^0-9]/g,"");
    }
}
</script>   

HTML

<input type='text' id='phone' name="phone"  maxlength="10"> 
<div id='phone_availability_result'></div>  

PHP Code&#39; check_phone.php&#39;

<?php
include('connnew.php');
//$phone = mysql_real_escape_string($_POST['phone']);  

$resultp = $usersdb->query("SELECT * FROM users WHERE Phone = '$phone'") or   die($usersdb->error);  

//if number of rows fields is bigger them 0 that means it's NOT available '  
if($resultp->num_rows>0){  
//and we send 0 to the ajax request  
echo 1;  
}else{  
//and we send 1 to the ajax request  
echo 0;  
}  
?>

我经常收到错误&#34;错误的东西&#34;。虽然当我独立运行check_phone.php文件时,它工作正常。我想在返回值函数中存在一些错误。

也许有人可以帮助识别错误。

2 个答案:

答案 0 :(得分:1)

  1. 在比较之前修剪响应值

    $ .post(&#34; check_phone.php&#34;,{phone:phone},

        function(resultp){  
            //if the result is 1  
            if($.trim(resultp) == 1){  
                //show that the phone is available  
                $('#phone_availability_result').html(phone + ' is Available');  
            }else if ($.trim(resultp) == 0){  
                //show that the phone is NOT available  
                $('#phone_availability_result').html(phone + ' is not Available');  
            }else{//show that the phone is NOT available  
                $('#phone_availability_result').html('Something Wrong');}  
        });  
    
    1. $ phone在此代码中发表了评论

          $resultp = $usersdb->query("SELECT * FROM users WHERE Phone = '$phone'") or   die($usersdb->error);  
      
      
          if($resultp->num_rows>0){  
      
          echo 1;  
          }else{  
      
          echo 0;  
          }  
      
         ?>
      

答案 1 :(得分:0)

可能是你收到字符串的回复,试试这个:

if(resultp == '1' || resultp == 1) {  
    //show that the phone is available  
    $('#phone_availability_result').html(phone + ' is Available');  
}else if (resultp == '0' || resultp == 0){  
    //show that the phone is NOT available  
    $('#phone_availability_result').html(phone + ' is not Available');  
}else{//show that the phone is NOT available  
    $('#phone_availability_result').html('Something Wrong');
}