2014-01-07 36 views
0

我使用codeigniter構建網站。登錄頁面有一個「忘記密碼」鏈接。當用戶點擊它時,系統會要求他輸入他的電子郵件,然後再進一步處理。我想對用戶輸入的電子郵件設置一個規則以檢查它是否存在在數據庫中是否存在。如果數據庫中不存在該電子郵件,則應該顯示一條錯誤消息,表明該電子郵件不存在並將用戶重定向到同一頁面。我是codeigniter的新手。請幫助我。提前致謝。這是我嘗試過的沒有成功的。在codeigniter中創建自己的表單驗證規則

view 'change password' 
<!DOCTYPE html> 
<html> 
<head> 
    <title> Login</title> 
    <link rel="stylesheet" href="http://localhost/cinema/assets/css/form.css"> 
</head> 
<body> 
<form action="http://localhost/cinema/verifyque/sec_que" method="post" accept-charset="utf-8" class="username"> 
    <br> 
    <p> 
      <label for="email_id">Email ID</label> 
     <input type="text" id="username" name="email_id"/> 
    </p> 

    <input type="submit" name="btnSubmit" class="styled-button-8" value="Submit" 
      /> 
     <font color="red" size="4"><b> 
     <?php echo validation_errors(); ?></b></font> 

</form></body></html> 


Controller 
    function sec_que(){ 
     $this->load->library('form_validation'); 
     $this->form_validation->set_rules('email_id', 'Email', 'callback_email_available'); 
      function email_available($str) 
{ 
// You can access $_POST variable 
$this->load->model('user'); 
$result = $this->user->emailAvailability($str); 
if ($result) 
{ 
    return TRUE; 
}else{ 
    $this->form_validation->set_message('email_available', 'The %s does not exist'); 
    return FALSE; 
} 
} 
if($this->form_validation->run() === TRUE) { 
     $this->load->model('user'); 
     $email['email_id'] = $this->input->post('email_id'); 
     $this->session->set_userdata($email); 
     $data['que_id_1']= $this->user->display_que(); 
     $data['que_id_2']= $this->user->display_que2(); 
     $this->load->view('forgot_password_2', $data); 

    } 
else{ 
$this->load->view('change_password'); 
} 
} 

Model 
    public function emailAvailability($email) 
{ 
    $this->db->where('user_email',$email); 
    $query = $this->db->get('users'); 
    return $query->row(); 
} 
+0

[創建自定義密碼驗證規則]的可能的重複(http://stackoverflow.com/questions/8181779/creating-a-custom-codeigniter-validation-rule) –

回答

1

下面是你需要使用的代碼.. 使用回調方法, 表單驗證:

$this->form_validation->set_rules('Email', 'Email', 'callback_emailAvailability'); 

型號:

public function emailAvailability($email) 
    { 
     $query = $this->db->get_where('user_email',$email); 
       if($query > 0){ 
       return true; 
       }else{ 
      return false; 
       } 
    } 

希望它能幫助。