Javascript message for validating username health

I'm trying to check if the register username is available, but every time the client returns his username.

This is my client code:

$(document).ready(function()
{
    $("#register_username").blur(function(){
    var user = $("#register_username").val();
    $.post("register",
    {
        username: user
    },
    function(data, status){
        if(data == '1')
        {
            alert('Good, username its available!');
        }
        else
        {
            alert('Snap!You cant use this username :(!');
        }
        });
    });
});

      

And this is the serveride code:

if(strlen($_POST['username']) > 0)
{
    $usr = $_POST['username'];
    if($usr == 'test')
        echo '1';
    else
        echo '2';
}

      

PHP version: 5.5

+3


source to share


1 answer


First of all, I think if the input field was "test", it would say "you cannot use this username"?

in your code if typing "test" ($usr == "test" - that comes from input)

you echo 1

, and in your code 1 means username is available ? So shouldn't it be back?



Does it say "username is available" with every input?

+2


source







All Articles