0

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 ?

2
Contributors
5
Replies
21
Views
4 Years
Discussion Span
Last Post by OsaMasw
0

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

Edited by pixelsoul

0

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.

0

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.

2

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

Edited by pixelsoul

Votes + Comments
worked !!
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.