How to Verify duplicate email validation using Ajax?

1.create a view page:-
verify.php

<body>

  <div class="container">

    <div class="title">Registration</div>

    <div class="content">

      <form action="<?php echo base_url();?>index.php/Welcome/registerusers" id="form_id" method="post">

        <div class="user-details">

          <div class="input-box">

            <span class="details">First-Name</span>

            <input type="text" placeholder="Enter your name" name="fname" id="fname">

            <span class="error"><?php echo form_error('fname');?></span>

          </div>

          <div class="input-box">

            <span class="details">Email</span>

            <input type="text" placeholder="Enter Your Email" name="email" id="email" >

            <span  class="error" id="message"><?php echo form_error('email');?></span>

          </div>

          <div class="input-box">

            <span class="details">Phone Number</span>

            <input type="text" placeholder="Enter Your Number " name="contact" id="contact">

            <span  class="error"><?php echo form_error('contact');?></span>

          </div>

  </form>

    </div>

  </div>

</div>

</body>

<!--Here Start Script code-->

<script>

$(document).ready(function()

{

$("#email").change(function()

{

var email = $('#email').val();


  if(email!="")

  {

    jQuery.ajax({

    type: "POST",

    url: "<?php echo base_url();?>index.php/Welcome/check_email_availability",

    data: {email: email},

    success: function(res)

    {

       //console.log(res,"success");

      if(res==1)

      {

      $("#message").css({"color":"red"});

      $("#message").html("This user already exists");

      }

      else

      {

      $("#message").css({"color":"green"});

      $("#message").html("Congrates username available !");

      }  

    },

    error: function(res)

    {

    alert('some error');  

    }

    });

  }

  else

  {

  alert("pls enter your email id ");

  }

});

});

</script>
2.Create Controller:-

<?php

defined('BASEPATH') OR exit('No direct script access allowed');


class Welcome extends CI_Controller {


    public function __construct()

    {

        parent::__construct();

        $this->load->database();

        $this->load->library('form_validation','session');

     }

public function check_email_availability()

    {

        $email=$this->input->post('email');

        $this->load->model('Verfify_Model ');

        $result=$this->Verfify_Model ->is_email_availabilty($email);

       

        if($result)

        {

            echo 1;

        }

        else

        {

            echo 0;

        }

    }  

?>

3.Create Model:-

<?php

class Verfify_Model extends CI_Model

{   

public function is_email_availabilty($email)

    {                

        $this->db->where('email',$email);

        $query=$this->db->get('users');

        if($query->num_rows() > 0)

        {

           return $query->row()->id;

        } else{


            return 0;

        }

    }

    ?>

Sign In or Register to comment.