2


I have an input field which I would like to dynamically validate while user is typing... I am using Codeigniter and I believe that I have some URI problem... Let me show you the code...

HTML/PHP:
<input type="text" id="username"> <span id="validateUsername"></span>

jQuery:

$(document).ready(function() {
    var validateUsername = $('#validateUsername');
        $('#username').keyup(function () {
        var t = this; 

        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);
            validateUsername.removeClass('error').html('<img src="<?php echo site_url("public/images"); ?>/ajax-loader.gif" height="16" width="16" /> checking availability...');
            this.timer = setTimeout(function () {
                $.ajax({
                    url: '<?php echo base_url(); ?>register/check_user/' + t.value,
                    dataType: 'json',
                    type: 'POST',
                    success: function (j) {
                        validateUsername.html(j.msg);
                    }
                });
            }, 200);

            this.lastValue = this.value;
        }
    });
});

My codeigniter controller has a very simple function:

function check_user($username) {
    // var_dumping stuff goes here
}

In there I'm just trying to var_dump different stuff (like $_SERVER, $_REQUEST, $username), but I think that my script doesn't even reach that controller...

I was trying to change url: '<?php echo base_url(); ?>register/check_user/' + t.value, line in my jQuery script but it seems that no combination works. For example:
url: '<?php echo site_url('register/check_user/'); ?>' + t.value,
url: 'index.php/register/check_user/' + t.value,
url: 'register/check_user/' + t.value,

...whatever I try my validateUsername span shows only "checking availability" message.

I was trying my script in Chrome, Safari and IE8. PHP version is 5.3.0 and jQuery is 1.4.2...

Any ideas what could be wrong here?

Thanks for any help in advance!

1 Answer 1

4

I see there could be multiple issues with your code:

are the routes properly set?

In your CodeIgniter routes, do the following:

$route['register/check_user/(:any)'] =" register/check_user/$1";

In your CodeIgniter controller, access the variable using this:

function check_user() {
    $username = $this->uri->segment(3);
    var_dump($username);
    // var_dumping stuff goes here
}

Also, I would suggest using jQuery Text Change event, It has worked great for me, instead of using setTimeouts and keyup.

Example:

$('#username').bind('textchange', function () {
   $.post('<?php echo base_url(); ?>register/check_user/' + this.value, function(data) {
      $('#validateUsername').html(data);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Hi and thanks for help! Yes, I actually DID forget about routing, but when I set it up it still didn't work :-/ I checked out jQuery Text Change and I'm using that plugin now... Very elegant and easy :) Thanks for link :)
+1, but worth mentioning function check_user() {$username = $this->uri->segment(3); can also be replaced with just function check_user($username). this has the added benefit of still working if you change the structure at some stage eg module/controller/method/$1 (the uri structure changes, meaning 3 would be invalid).

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.