0

I have a problem in running the following script . It runs correctly except for the AJAX part . It doesnt makes any call to the url "check_username.php" . I have checked the database connection .

Any help would be highly appreciated . Thanks

jquery code -

<script type="text/javascript">


$(document).ready(function() {


        //the min chars for username
        var min_chars = 3;

        //result texts
        var characters_error = 'Minimum amount of chars is 3';
        var checking_html = '<img src="images/loading.gif" /> Checking...';

        //when button is clicked
        $('#check_username_availability').click(function(){
            //run the character number check
            if($('#username').val().length < min_chars){
            //if it's bellow the minimum show characters_error text
            $('#username_availability_result').html(characters_error);
        }else{          
            //else show the cheking_text and run the function to check
            $('#username_availability_result').html(checking_html);
            check_availability();
        }
    });


});

//function to check username availability   
function check_availability(){  

    //get the username
    var username = $('#username').val();

    //use ajax to run the check

     $.post("check_username.php", { username: username },   //This part is not being executed . 
        function(result){
            //if the result is 1
            if(result == 1){
                //show that the username is available
                $('#username_availability_result').html('<span class="is_available"><b>' +username + '</b> is Available</span>');
            }else{
                //show that the username is NOT available
                $('#username_availability_result').html('<span     class="is_not_available"><b>' +username + '</b> is not Available</span>');
                }
        });

}  
</script>`

check_username.php -

<?php


mysql_connect('localhost', 'root', '');
mysql_select_db('modal');


$username = mysql_real_escape_string($_POST['username']);


$result = mysql_query("SELECT username FROM users WHERE username=`$username`");
$num_rows = mysql_num_rows($result);


//if number of rows fields is bigger them 0 that means it's NOT available '
if($numrows>0) {
echo 0;

}else{

    echo 1;
}

?>
  • Any error in your console? – leonardeveloper Nov 23 '13 at 22:19
  • Use the developer console in the browser of your choice to see what the actual response from the server for the $.post request is, and what parameters are supplied. Your SQL-query is wrong either was, as it would retrieve all the users in the table and not just the username you're looking for. – MatsLindh Nov 23 '13 at 22:22

1 Answers1

0

Your query returns all rows from users since you didn't specify any specific. So $result will hold the first row retrieved (and you can't be sure which that is). You are basically checking a random row for the username. If you wanted to iterate over all rows, you could do this with while ($result = mysql_fetch_array(mysql_query(..))) but I would suggest making use of SQL.

$result = mysql_query("SELECT username FROM users WHERE username=`$username`");
$num_rows = mysql_num_rows($result);
echo $num_rows;
  • The query

    SELECT username FROM users WHERE username=`$username`
    

    will now all rows where username==$username. So you will get either 0 or 1 row back (all in all, instead of #of_users before).

  • mysql_num_rows will count the number of returned rows which is either 0 or 1, so you can directly print it.

Also keep in mind that mysql_* functions are officially deprecated and hence should not be used in new code. You can use PDO or MySQLi instead. See this answer on SO for more information.

Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51