Andrei Vlad
Andrei Vlad

Reputation: 315

Javascript post to check username availability not working

Im trying to check if the register username's available but everytime the clientside returns that the username its available.

This is my clientside 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 serverside code:

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

PHP Version: 5.5

Upvotes: 3

Views: 153

Answers (1)

inubs
inubs

Reputation: 519

first of all, i guess if the input field has value "test" it would say "you cannot use this username"?

in your code if input is "test" ($usr == "test" - that comes from input) you echo 1, and in your code 1 means username is available? So shouldn't it be backwards?

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

Upvotes: 2

Related Questions