0

I m trying to check if a username exists or not but even if the username doesn't exists it keeps saying "Username already exists" this is my javascript code:

$.validator.addMethod("checkUsername", 
    function(value, element) {
        var test = "";
        $.ajax({
            type:"GET",
            url: site_url + "/ajax/checkusername/" + value,
            success: function(result){
               if(result=="exists")
                   return false;
               else
                   return true;
            }
        });
    }, 
    "Username Already Exists."
);



    $("#myform").validate({
        rules: {
            username2: {
                required: true,
                minlength: 6,
                checkUsername:true
            },
            password2: {
                required: true,
                minlength: 6
            },
            email: {
                required: true,
                email: true
            }
        }
    });
});

This is my controller code:

public function checkusername($username)
{
            $this->load->model('user_model');
            $user = $this->user_model->getUser($username);
            if($user == null)
            {
                echo json_encode("notexists");
            }
            else
            {
                echo json_encode("exists");
            }  
}

Any Ideas about this how this can be solved? Thanks in advance.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Trustos
  • 41
  • 3
  • 11

2 Answers2

4

Why are you encoding the response as JSON when you're not parsing JSON with your ajax?

What happens when you simply do this?:

public function checkusername($username)
{
        $this->load->model('user_model');
        $user = $this->user_model->getUser($username);
        if($user == null)
        {
            echo "notexists";
        }
        else
        {
            echo "exists";
        }  
}

EDIT:

$.validator.addMethod("checkUsername", 
    function(value, element) {
        var result = false;
        $.ajax({
            type:"GET",
            async: false,
            url: site_url + "/ajax/checkusername/" + value,
            success: function(msg) {
                result = (msg == "exists") ? false : true;
            }
        });
        return result;
    }, 
    "Username Already Exists."
);
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • Tried it still same problem – Trustos Apr 27 '13 at 15:31
  • @Trustos, I'm doing nearly the exact same thing with my CodeIgniter & `ajax` and it's working fine for me. The only difference is that mine is using `type = "post"`. Do a console log in your `ajax` to see what the response looks like. Add `console.log(result);` as the first line inside your `success` callback function. Then look at your JavaScript console to see the exact value being returned. – Sparky Apr 27 '13 at 15:50
  • It returns exist and notexist the values it should. Its like the error message Username already exists just keeps on popping up when it shouldn't – Trustos Apr 27 '13 at 16:27
2

$.ajax read a string. Cause you do echo json_encode this string is json.

You can use $.getJSON in stead of $.ajax

$.getJSON( site_url + "/ajax/checkusername/" + value, function( json ) {
  if(json=="notexists"){alert('Not exists')}
 });

or add $.parseJSON to your original code:

  if($.parseJSON(result)=="exists")

update: i had to change my answer your function doesn't return the result of your $.ajax. $.ajax had to set your return value. Cause can't return before your ajax has been finished, you also have to add async: false to your request (see: How do I make jQuery wait for an Ajax call to finish before it returns?)

$.validator.addMethod("checkUsername", 
    function(value, element) {
        var test = false;
        $.ajax({
        type:"GET",
        async: false,
        url: site_url + "/ajax/checkusername/" + value,
        success: function(result){
           if(result=="notexists") {test = true; }       
           }
    });
    return test;
    }, 
    "Username Already Exists."
);
Community
  • 1
  • 1
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
  • what is the result when you load site_url + "/ajax/checkusername/" + value in a browser? – Bass Jobsen Apr 27 '13 at 14:07
  • If I do this one http://localhost/virtualclubmanager/index.php/ajax/checkusername/aaaaaaa it returns "nonexists" because its not in database If I do this one http://localhost/virtualclubmanager/index.php/ajax/checkusername/testing It returns "exists" because it is in the database – Trustos Apr 27 '13 at 14:21