Codeigniter Tutorial: How To check Username/Email availablity using jQuery in Codeigniter?

This post is for CI lover who wants to learn Codeigniter. This script will show you how can we check live availability of username or email using Ajax in Codeigniter. Using this tutorial you can also understand how can we send a jQuery Ajax request to server living in CI.
In your view where you have created form include jQuery library and put the below code there.
In header section:
$(document).ready(function() {
/// make loader hidden in start
$('#Loading').hide();
$('#email').blur(function(){
var a = $("#email").val();
var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
// check if email is valid
if(filter.test(a)){
// show loader
$('#Loading').show();
$.post("controller_name/check_email_availablity", {
email: $('#email').val()
}, function(response){
//#emailInfo is a span which will show you message
$('#Loading').hide();
setTimeout("finishAjax('Loading', '"+escape(response)+"')", 400);
});
return false;
}
});
function finishAjax(id, response){
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
In body section in your form:
In Your controller add this function:
function check_email_availablity()
{
$this->load->model('My_model');
$get_result = $this->My_model->check_email_availablity();
if(!$get_result )
echo 'Email already in use. ';
else
echo 'Email Available';
}
Suppose you have a model called my_model.php and a table tbl_members or what ever you named. This table should have a field named “email” of type varchar [200].
Add this function:
function check_email_availablity()
{
$email = trim($this->input->post('email'));
$email = strtolower($email);
$query = $this->db->query('SELECT * FROM tbl_members where email="'.$email.'"');
if($query->num_rows() > 0)
return false;
else
return true;
}
Thats it, this function will return a message against email field that whether it is available or not. This is not for only email, you can use it to check any field. Also this is client side check so you also need to use this model function in server side so that email can also be verified when form has been submitted with disabling javascript .
20 to “Codeigniter Tutorial: How To check Username/Email availablity using jQuery in Codeigniter?”
Post comment

Categories
- AJAX (28)
- Announcement (3)
- Blogging Tips (1)
- Codeigniter (16)
- CSS (14)
- Facebook (8)
- Freelance Tips (1)
- How-To (2)
- Joomla (1)
- JQuery (51)
- Miscellaneous (3)
- Mootools (1)
- MySQL (3)
- PHP (57)
- SEO (2)
- Technology (6)
- Tutorials (17)
- Twitter (2)
- Web Design (26)
- Web Development (58)
- WordPress (3)
Popular post
- 46 Highly Responsive Admin Templates for Your Websites
- $50 PayPal Cash & 5 Premium PHP Wall Script Prizes #Giveaway
- PHP Wall Script Clone with Real Time Features __ January 2013 Release __
- Things you should know if you are a Blogger
- Top 10 Must Have Qualities of a Freelance Web Developer
- Fundamental Factors for Mobile Compatible eCommerce Hosting
- Facebook Wall Script New Version in September 2012
Recommend

Try to avoid referencing your post values in the model – you should pass the value from the controller.
@dan, thanks dan, actually i use a single function to add update my records so no need to pass post values twice from controller. I just get them in my model.
That’s really not the best way (in my humble opinion) to do what you are trying to do. But you know what – there are many different ways you can achieve the same thing and I’m sure I do HEAPS of things in a sub optimum way!
But apart from what Dan has suggested which I also believe is good advice… You also have some problems with SQL Injection and XSS. I would recommend that you get the email POST using $this->input->post(‘email’, TRUE) which will perform a check for XSS hack. Then furthermore to prevent SQL injection I would do your query like this: $this->db->query(‘SELECT * FROM BLAH WHERE email = ?’, $email);
Also in your JQuery – seems to me that you don’t do anything if the email address doesn’t pass the test however at this point they have left the INPUT. So not doing anything seems counterintuitive? Shouldn’t you put a message up saying invalid email or something? or why bother at all – simply check that in the controller using form_validation library. If you did you could apply the trim, xss_clean, valid_email etc all in one go which would be nice too?
Anyway hope this helps and as I said – it will still work – just might not be the best approach!
Cheers,
Al
@Al, I agreed with you Al
I use these things after keeping all security points in my mind. SQL injection and XSS attacks can also be controlled thru global config variable. If we use active class library of CI to interact with db then sql injection can be eliminated.
I just tried to explain a simple way to CI developers that how can we make it happen, while these things comes under security issues and one day I will write a tutorial on implementing CI security in a project and I would also keep this point in my mind when ever I will write next tutorial so that I can keep you people in peace
Cheers,
Zee
[...] Codeigniter Tutorial: How To check Username/Email availablity using jQuery in Codeigniter? [...]
hi this is not working ..
‘v’ like
Dude nice article, although i wonder why your doing this in this manner as you should know CI can do this for you without writing more than a line of code….. ??
Hi
You should catch value from controller and then pass model.
to be suggestion.
[...] Vielzahl von Tutorials zu CodeIgniter mit jeweils sehr interessanten Themen, unbedingt reinschauen.How To check Username/Email availablity using jQuery in CodeIgniter? Upload and Convert video to FLV using FFmpeg. Simple class to encrypt url data How to Create Ajax [...]
hi Zeeshan…..
i am web developer. Fresher in industry. recently working on php project . i have one project our team decided to use codeigniter in that….
until now i develop application withuot using mvc ..
it is helpful to develop application with codeigniter….
if you have any information site please help me…
Reply soon
[...] How to check username/email availability with jQuery in CI: http://www.99points.info/2010/07/codeigniter-tutorial-check-usernameemail-availablity-using-jquer-in… [...]
CI handles SQL and XSS attacks by itself already, also, I wouldn’t use a model in this case.
It’d be rather useful if your function (CI one), would respond with a JSON status and highlight e-mail field depending on it’s response, or disabling the submit button if it’s in use.
Besides that, I love the fact you’re promoting CI and Jquery! Those two go very well together!
Hi dear Zeeshan i am new to Framework. Please refer me some E-book or some readable material, which can help me as i am totally new to framework, i was working on Plain PHP, i have just move my concentrations to this recently. I need help.
Experts answers will be awarded.
its not working
How can I search if email or user exist while using jquery validate ?
remote: “email.php”
must i just select the e-mail in the database and automatically my script will detect if it exist or I must create a function etc.
you can not print echo´s in a controller!
it doesn´t make sense with the MVC schema! you can use another view where to print the responses and load that view troguht that you can obtain the response in html or plain text and use it however you want in your actual page (view) ussing obviously!
There are certainly a lot of information like that to take into account. That is a fantastic factor to carry up.
This system will describe to you how can we assess remain choice sign in name or e-mail using Ajax in Codeigniter. Using This information we can also comprehend how can we provide a jQuery Ajax ask for to web comprehensive wide range web comprehensive wide range web wide range web variety web host hosting server living in CI.
Really helpful buddy thanx alot for this post