Reputation: 147
I have created a form for add user which contain email, password, firstname, and lastname. I want to verify the email to see whether it is already exit or not yet?
How can I do that? any help will be appreciate.
Upvotes: 0
Views: 5189
Reputation: 73
you can create a callback function to check wether an email exist or not using your model like that Controller
$this->form_validation->set_rules("email","Email","required|callback_isEmailExist");
now create a callback function in your controller
function isEmailExist($email)
{
$verifyemail = $this->model_name->verifyemail($email)
if($verifyemail == true)
{
return false;
}
else{
return true;
}
}
Model
function verifyemail($email)
{
$this->db->select("*");
$this->db->from("table_name");
$this->db->where("email",$email);
$query = $this->db->get();
if($query->num_rows() == 1)
{
return true;
}
else{
return false;
}
}
Upvotes: 1
Reputation: 1332
If you are using the form_validation of codeigniter you should add a rule as follow:
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
Upvotes: 0
Reputation: 1067
please try this --
controller -- school.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class School extends CI_Controller {
public function registration()
{
$this->load->view('registration');
}
public function insert()
{
$data = $_POST;
$email = $data['email'];
$result = $this->school_model->checkEmail($email);
if(empty($result))
{
$this->school_model->insertData($data);
$msg = "Data Insert Successfully";
}
else
{
$msg = "Email Already Exists";
}
$this->session->set_flashdata('msg', $msg);
redirect('school/registration');
}
}
?>
model -- school_model.php
<?php
class School_model extends CI_Model
{
public function checkEmail($email)
{
$this -> db -> select('*');
$this -> db -> from('users');
$this -> db -> where('email', $email);
$query = $this -> db -> get();
return $query->result_array();
}
public function insertData($data)
{
$this->db->insert('users', $data);
}
}
?>
view -- registration.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="show_flash_msg"><?php echo $this->session->flashdata('msg'); ?></div>
<form method="post" action="<?php echo base_url(); ?>school/insert">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
Password: <input type="text" name="password">
Mobile: <input type="text" name="mobile">
<input type="submit" value="Submit">
</form>
</body>
</html>
Upvotes: 0