Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 404
Andrei Vlad Avatar asked Aug 22 '26 00:08

Andrei Vlad


1 Answers

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?

like image 124
inubs Avatar answered Aug 24 '26 12:08

inubs