How can We verify Captcha with Ajax in Codeigniter?
Captcha is necessary part of any form. But if we see with end user point of view, a user feels bad when he/she has to give a right verification code and also have to face an error message if it is invalid code. “I don’t like Captcha and even i didn’t design any form with Captcha” ( Said by: AdbulRehman (a creative Graphic Designer ) .
If we give captcha verification using ajax, then it reduces user’s troubles.
We can check captcha on blur event or on submitting a form and alert the user about invalid code entered.
In Codeigniter ( i used it CI in some place ) it is easy to do that:
In first step you will show captcha using CI method in simple way. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function Ajax_captcha(){
var code = $("#code").val(); // get the code entered
if(code.length>1){
$('#Loading').show(); // show the loader image
$.post("<?php echo base_url()?>Site/Ajax_Captcha",{
code: $('#code').val()
}, function(response){
$('#Ifno').fadeOut();
setTimeout("RemoveAJAXCaptcha('Info', '"+escape(response)+"')", 400);
});
}
return false;
}
// this function will hide the loader image and show the result as inner html
function RemoveAJAXCaptcha(id, response){
$('#Loading').hide();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
} |
In html:
1 2 3 4 5 | <input id="code" name="code" type="text" value="" onblur="return Ajax_captcha();" /> <span id="Info"></span> <span id="Loading"> <img src="<?php echo base_url()?>images/loader.gif" alt="" /> </span> |
And in your controller:
1 2 3 4 5 6 7 8 9 10 11 | function Ajax_Captcha($code,$this->session->userdata('yourSessionVarOfCaptcha')) { if ($code==strtoupper($this->session->userdata('yourSessionVarOfCaptcha'))) { return true; } else { return false; } } |
Who I Am

Zeeshan Rasool
Software Engineer - PHP
Lahore - Pakistan
zeeshan(@)99points.info
Skype: zeeshan-rasool
gTalk: zishan.rasool85
Categories
- AJAX (37)
- Codeigniter (16)
- CSS (16)
- Facebook (11)
- Joomla (1)
- JQuery (53)
- Miscellaneous (4)
- Mootools (1)
- MySQL (6)
- PHP (60)
- SEO (2)
- Technology (6)
- Tutorials (15)
- Twitter (2)
- Web Design (23)
- Web Development (57)
- WordPress (2)
Tags
Comments
- ZeeShaN on Facebook Wall Script Clone with JQuery and PHP: Personal Project BETA Version 2.0
- 20 + Fresh jQuery Image Gallery/Slider Plugins and Tutorials Worth a Look | free on JQuery Based Flipped Image Gallery with Bounce Effects
- 20 + Fresh jQuery Image Gallery/Slider Plugins and Tutorials Worth a Look | free on Fresh JQuery Image Gallery with Captions and Auto Play/Pause Rotation
- 25 Cool and Helpful jQuery Plugins/Tutorials For Your Next Project | free on jQuery Tutorial: Create jQuery and CSS based Awesome navigation.
- 25 Cool and Helpful jQuery Plugins/Tutorials For Your Next Project | free on Ajax Tutorial: How to Create Ajax Search Using PHP jQuery and MYSQL
ZeeShaN







[...] Verify captchas with AJAX in codeigniter: http://www.99points.info/2010/03/verify-captcha-with-ajax-using-codeignite/ [...]
XD
If only I had a nickel for each time I came to http://www.99points.info! Great article.
Wow I’m literally the first reply to your amazing read?!
Thanks for your post
Nice idea but i have a problem that if i check a code valid then i have to again click on submit button to submit the form. i means it needs twice. can you provide any way thru which we can submit form automatically after finding it valid?