user3526204
user3526204

Reputation: 509

Ajax Post not returning proper value

I am trying to verify if phone number exists in database when user fills in a number into the form field. I am using Ajax for post and return confirmation.

Everything seems fine except for an error when returning value from external file.

I am not able to pinpoint the exact error reason

The code is as follows

<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 'check_phone.php'

<?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;  
}  
?>

I am constantly getting the error "Something Wrong". Though when I independently run the check_phone.php file, it works fine. I guess it is in the return value function that there is some error.

Maybe somebody can help identify the bug.

Upvotes: 0

Views: 97

Answers (2)

Varun Malhotra
Varun Malhotra

Reputation: 1202

  1. trim the response values before compare

    $.post("check_phone.php", { 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 is commented in this code

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

Upvotes: 1

Rijin
Rijin

Reputation: 934

May be you are getting response as string, Try this :

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');
} 

Upvotes: 0

Related Questions