Codeigniter ajax check email availability using jquery - Mostlikers

07 July, 2016

Codeigniter ajax check email availability using jquery

Codeigniter ajax check unique email availability using jquery it's very simple script. For checking the unique email id or username CodeIgniter have default validation function. it will check ajax controller response whether that email id exists or not to the particular table column. let see the code for that.




Live Demo                  Download


Validation rules

$this->form_validation->set_rules('email','Email','required|is_unique[tablename.field]');

Codeigniter validation rules is_unique function will check particular table field value. 'users' it's a table name.

Database

Create sample table name called users and below contain the column fields.
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL,
  `email` varchar(100) NOT NULL,
  `username` varchar(250) NOT NULL,
  `password` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Set your database connection config/database.php file.

Controller

welcome.php

<?php 
class Welcome extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
    } 

    public function ajax_email_check()
    {
        $this->load->view('signup_check');
    }

    public function ajax_signup()
    {
        
        $this->form_validation->set_rules('email', 'Email', 
        'required|is_unique[users.email]');
        $this->form_validation->set_message('is_unique', 'The %s is already taken');
        if ($this->form_validation->run() == FALSE):
                echo 'The email is already taken.';          
        else :           
            unset($_POST);
            echo 'Available';
        endif;
    }

}


Folder path : applications/controllers/welcome.php
Url access path : http://your hostname/project folder/index.php/welcome/ajax_email_check
is_unique - Returns FALSE if the form element is not unique to the table and field name in the parameter. Note: This rule requires Query Builder to be enabled in order to work.

View

signup_check.php


<form id="reg_form">  
  <h5>Email Address</h5>
  <input type="text"  name="email" value="<?php echo set_value('email'); ?>" />
  <div class="ajax_response_result"></div>
  <input type="button" class="btn btn-primary" id="click_form" value="Submit" />
  </div>
</form>

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $(document).on('click','#click_form',function(){
    jQuery.ajax({
    type: "POST",
    url: "<?php echo site_url('welcome/ajax_signup') ?>",    
    data: $("#reg_form").serialize(),
    success: function(res) {
     $(".ajax_response_result").html(res);
     }
    });
  });
});
</script>

Validation error message -  $('.ajax_response_result').html(res);

In this tutorial, i have explained about basic of email id validations. My next tutorial I will guide how to write form validation and multiple form validation rules. I hope this tutorial helpful for you. 









Related Topics


1 comment: