Jul
4

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$(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("<?php echo base_url()?>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:

1
2
3
4
5
<div>
	<label>E-mail</label>
	<input id="email" name="email" type="text" value=""  />
	<span id="Loading"><img src="loader.gif" alt="Ajax Indicator" /></span>	
</div>


In Your controller add this function:

1
2
3
4
5
6
7
8
9
10
function check_email_availablity()
{
	$this->load->model('My_model');
	$get_result = $this->My_model->check_email_availablity();
 
	if(!$get_result )
	echo '<span style="color:#f00">Email already in use. </span>';
	else
	echo '<span style="color:#0c0">Email Available</span>';
}

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:

1
2
3
4
5
6
7
8
9
10
11
12
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 .

Add To Facebook Stumble This Digg This Add To Del.icio.us Add To Reddit Add To Yahoo Add To Twitter


Written by ZeeShaN

ZeeShaN RasooL is a web developer who loves to work in latest technologies to create more interactive dynamic and beautiful web pages.


  • 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!

    January 16 2012
    CommentsLike
      • abdul shaikh

        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

        December 07 2011
        CommentsLike

        • [...] 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 [...]

          October 31 2011
          CommentsLike

          • Hi

            You should catch value from controller and then pass model.

            to be suggestion.

            June 02 2011
            CommentsLike

            • 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….. ??

              March 18 2011
              CommentsLike
              • meemeo

                ‘v’ like

                January 24 2011
                CommentsLike
                • rav

                  hi this is not working ..

                  January 06 2011
                  CommentsLike

                  • [...] Codeigniter Tutorial: How To check Username/Email availablity using jQuery in Codeigniter? [...]

                    July 08 2010
                    CommentsLike
                    • ZeeShaN

                      @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

                      July 06 2010
                      CommentsLike








                      Enter your Email:

                      Click Here for Popular

                      Who I Am

                      Zeeshan Rasool

                      Software Engineer - PHP
                      Lahore - Pakistan

                      zeeshan(@)99points.info
                      Skype: zeeshan-rasool
                      gTalk: zishan.rasool85

                      Categories

                      Tags

                      Comments