0

I am trying to make a ajax request to check the database if this username is available, but when trying to run the script in the console it give me an error that the is an Uncaught SyntaxError- Unexpected Identifier. I have checked my code but can not find the problem. Can someone please help me figure this out?

Here is my code.

main.js

$('#checkusername').keyup(function() {
    $.ajax({
        var username = $('#checkusername').val();

        url: 'usernamecheck-validate.php',
        type: 'POST',
        data: {checkusername: username},
        success: function(data){
            $('.status').html(data);
        }
    });

});

register.php

 <form action="#" method="POST">
    <input type="text" name="email" id="email" class="form" placeholder="Enter Email"><br><br>
    <input type="text" name="username" id="checkusername" class="form" placeholder="Enter Username"><br><br>
    <input type="password" name="password" id="password" class="form" placeholder="Enter Password"><br><br>
    <input type="submit" name="register-validation-button" class="form-btn" value="Register Account">
 </form>
1
  • 2
    You can't have a var username = ... statement inside an object ({}). Commented Dec 30, 2015 at 16:33

2 Answers 2

2

You need to define username outside of the object you provide to the $.ajax call:

$('#checkusername').keyup(function() {
    var username = $('#checkusername').val();
    $.ajax({
        url: 'usernamecheck-validate.php',
        type: 'POST',
        data: {
            checkusername: username
        },
        success: function(data){
            $('.status').html(data);
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much i changed it and it started working like a charm. Once again i appreciate the help you provided.
No problem, glad to help.
2

you can't use var in ajax like you did in OP .. use it like this

$('#checkusername').keyup(function() {
   var username = $('#checkusername').val();
    $.ajax({
        url: 'usernamecheck-validate.php',
        type: 'POST',
        data: {checkusername: username},
        success: function(data){
            $('.status').html(data);
        }
    });

});

1 Comment

Thank you it is working now. Once again thank you for the help.

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.