Check Email availability using Ajax in CodeIgniter
Controller - Email_Controller.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Email_Controller extends CI_Controller {
//functions
function index()
{
$data["title"] = "Codeigniter Tutorial - Check Email availibility using Ajax";
$this->load->view("email_availibility", $data);
}
function check_email_avalibility()
{
if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
{
echo '<label class="text-danger"><span class="glyphicon glyphicon-remove"></span> Invalid Email</span></label>';
}
else
{
$this->load->model("main_model");
if($this->main_model->is_email_available($_POST["email"]))
{
echo '<label class="text-danger"><span class="glyphicon glyphicon-remove"></span> Email Already register</label>';
}
else
{
echo '<label class="text-success"><span class="glyphicon glyphicon-ok"></span> Email Available</label>';
}
}
}
}
?> Models - main_model.php
<?php
class Main_model extends CI_Model
{
function is_email_available($email)
{
$this->db->where('email', $email);
$query = $this->db->get("tbl_user");
if($query->num_rows() > 0)
{
return true;
}
else
{
return false;
}
}
}
?> Views - email_availibility.php
<!DOCTYPE html>
<html>
<head>
<title>Raushtech | <?php echo $title; ?></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<div class="container" style="width:600px">
<br /><br /><br />
<h3 style="color:red;"><?php echo $title; ?></h3>
<br />
<label>Enter Email</label>
<input type="text" name="email" id="email" class="form-control" />
<span id="email_result"></span>
<br /><br />
<label>Enter Password</label>
<input type="text" name="password" id="password" class="form-control" />
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#email').on('input', function(){
var email = $('#email').val();
// alert(email);
if(email != '')
{
$.ajax({
url:"<?php echo base_url(); ?>Email_controller/check_email_avalibility",
method:"POST",
data:{email:email},
success:function(data){
$('#email_result').html(data);
}
});
}
});
});
</script>
Comments
Post a Comment