bhuvana
bhuvana

Reputation: 59

Message: Undefined variable: email under Controllers

I am getting Undefined Variable email under Controllers.

Controller : login.php

public function index() {
        $this->load->view('bootstrap/header');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->load->model('Login_db');
        $is_exist = $this->Login_db->isEmailExist($email);
        if ($is_exist) {
            $this->form_validation->set_message('isEmailExist', 'Email Address Already Exists!');
            return FALSE;
        } else {
            return TRUE;
        }


        $this->load->view('bootstrap/footer');
    }

Model : login_db.php

public function isEmailExist($email) {
        $this->db->select('user_id');
        $this->db->where('email', $email);
        $query = $this->db->get('login');
        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

I have to check whether email exists are not.

Upvotes: 1

Views: 370

Answers (3)

Swarna Sekhar Dhar
Swarna Sekhar Dhar

Reputation: 548

by checking your code i came to assume that you want to allow only the emails which is already not in table ? why have not you validated with is_unique like

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

or you can change the line as

 $this->Login_db->isEmailExist($this->input->post('email'));

or you should define $email before passing it / calling the function

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

for custom massaging :

$this->form_validation->set_rules( 'email', 'Email', 'required|valid_email|is_unique[login.email]', array( 'is_unique' => 'Email already exists' ) );

better you go through the manual https://www.codeigniter.com/user_guide/libraries/form_validation.html

Upvotes: 0

Elena Roman
Elena Roman

Reputation: 127

before

$is_exist = $this->Login_db->isEmailExist($email);

add this (in case of a POST request)

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

or this ((in case of a GET request)

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

Upvotes: 2

GrenierJ
GrenierJ

Reputation: 1140

the var $email used here

$is_exist = $this->Login_db->isEmailExist($email); line 5 of index

is never isntanciate. You should instantiate it to avoir error.

Upvotes: 0

Related Questions