Skip to content

codeigniter ajax username/email availability check using JQuery

Last updated on February 1, 2018

Here is the simple script to check live username or email availability using jQuery’s Ajax method in CodeIgniter. you can also apply this logic/method to any other filed check.

Controller : application/controller/User.php
We are using in build CodeIgniter form validation library’s is_unique() function to check email/username availability. So I have not created user model. In the blow controller, you can find both server side and client side email/username validations. and one more thing I have used ci’s output class to echo output to ajax response, with and by using output class we can avoid 500 internal errors with compression enabled mode in codeigniter.

load->helper(array(
            'form',
            'url'
        ));
        // load database
        $this->load->database();
        // load form validation library
        $this->load->library('form_validation');
    }
	
    public function email_check()
    {
	    // allow only Ajax request	
        if($this->input->is_ajax_request()) {
		// grab the email value from the post variable.
		$email = $this->input->post('email');
		// check in database - table name : tbl_users  , Field name in the table : email
		if(!$this->form_validation->is_unique($email, 'tbl_users.email')) {
		// set the json object as output 				 
		 $this->output->set_content_type('application/json')->set_output(json_encode(array('message' => 'The email is already taken, choose another one')));
			}
		}
	}

	

}

/* End of file User.php */
/* Location: ./application/controllers/User.php */

View : application/views/user.php
In the view we have created a form included jQuery library and our custom jQuery to send request to server and to handle the response.We are sending Ajax request only if the given input is valid email address.



  
    
    
    
    arjunphp.com
  
  
  
    
Ajax Indicator

That’s it.

0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments