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!
I see there could be multiple issues with your code:
are the routes properly set?
In your CodeIgniter routes, do the following:
In your CodeIgniter controller, access the variable using this:
Also, I would suggest using jQuery Text Change event, It has worked great for me, instead of using setTimeouts and keyup.
Example: