I don't know whats wrong but my code not working properly

here is my html code

<input type="text" name="username" id="username">
<div id="check_usr"></div>

AJAX call

<script>

    $(document).ready(function() { //user name 
    $('#username').on("keypress", function(e)
    {       
        startTypingTimer($(e.target));

    });     
});

var typingTimeout;
function startTypingTimer(input_field)
{   
    if (typingTimeout != undefined) 
        clearTimeout(typingTimeout);
    typingTimeout = setTimeout( function()
                {               

                    if($('#username').val().length < 4 ){
                    $('#check_usr').empty().append('<span class="label label-important">Username must be more than 3 charachters</span>');

                    }else {
                              $.ajax({
                              url: "checkuser.php",
                              type: 'POST',
                              data : {username : $('#username').val()},
                              success:function(data){
                               if(data === 'yes'){
                               $('#check_usr').empty().append('<span class="label label-warning">Username is already taken</span>');
                              }else{
                              $('#check_usr').empty().append('<span class="label label-success">OK</span>');
                              }
                             }});



                    }
                }
    , 500);
}
</script>

and php file

<?php
include ('config.php');
if ($_POST) {
$user = mysql_real_escape_string($_POST['username']);
$sql = "SELECT user FROM `users` WHERE `user` = '".$user."'";
$run = mysql_query($sql);
if ($run) {
return 'yes';
unset ($sql,$run,$user,$_POST);
}
}
?>

Every time I ran the code the text "OK" appears even if the user found in table
tell me whats wrong ?

Recommended Answers

All 5 Replies

$run is always going to return TRUE if it runs, regardless if it has results.

I would check if the numbers of rows returned was greater than "0".

<?php
include ('config.php');
if ($_POST) {
$user = mysql_real_escape_string($_POST['username']);
$sql = "SELECT user FROM `users` WHERE `user` = '".$user."'";
$run = mysql_query($sql);
$num_rows = mysql_num_rows($run);
if ($num_rows > 0) {
return 'yes';
unset ($sql,$run,$user,$_POST);
}
}
?>

still the same :( , I've tried the php page alone and its working fine, it give me the correct answer
but in ajax always I get "OK" message.

It is strange, I debugged your script and had it echo the data that your php was sending back and it was correct. So, jQuery is getting the data back but for some reason it is not updating your div with the correct message.

Well I can't figure out why yours "doesn't" work, but I can change your code around to the way I would right it and it then does work. So if you want to use it cool, sorry I couldn't figure out why yours was busted. I might just be really tired.

<script>
$(document).ready(function() { //user name 

    $('#username').keyup(nameCheck);

});

function nameCheck()
{
    var username = $('#username').val();

    if(username.length < 4)
    {
        $('#check_usr').html('<span class="label label-important">Username must be more than 3 charachters</span>');
    }
    else
    {
        $.ajax({
            url: "checkuser.php",
            type: 'POST',
            cache: false,
            data: 'username='+ username,
            success: function(data)
            {
                if(data > 0){
                    $('#check_usr').html('<span class="label label-warning">Username is already taken</span>');
                }else{
                    $('#check_usr').html('<span class="label label-success">OK</span>');
                }
            }
        });
    }
}

</script>

and the PHP

<?php
if ($_POST){
    $user = mysql_real_escape_string($_POST['username']);
    $sql = "SELECT user FROM users WHERE user = '".$user."'";
    $run = mysql_query($sql);
    $num_rows = mysql_num_rows($run);
    echo $num_rows;
}
?>
commented: worked !! +2

Thanks for the code and its worked perfect, but I hope sombody tell me whats wrong in my code,
really appreciate the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.