0

I scanned a lot of answers here but didn't find a good answer. I'm new to PHP and JavaScript and want to create a variable (for comparison purposes) from a jQuery return value. I'm creating a registration system where I use this to check username availability:

$(document).ready(function() {
    $("#username").keyup(function (e) {

        //removes spaces from username
        $(this).val($(this).val().replace(/\s/g, ''));

        var username = $(this).val();
        if(username.length < 2){$("#user-result").html('');return;}

        if(username.length >= 2){
            $("#user-result").html('<i class="fa fa-refresh fa-spin"></i>');
            $.post('core/check_username.php', {'username':username}, function(data) {
             $("#user-result").html(data);
            });
        }
    }); 
});

I can display the return value in a span but for form validation purpose, I need to compare this return value with a set of criteria. How can I declare a variable from this return value?

0

1 Answer 1

1

in your $.post function you get data returned from your ajax-call. just use this to do further stuff with the username:

$.post('core/check_username.php', {'username':username}, function(data) {
    $("#user-result").html(data);

    var username = data;

    if(username == "xxx")
    {
        // do some stuff here
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.