1

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.

bhuvana
  • 59
  • 6
  • 4
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – atymic Jul 25 '19 at 07:33

3 Answers3

2

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');
Elena Roman
  • 137
  • 8
0

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.

GrenierJ
  • 1,145
  • 8
  • 18
  • But it is a function under model : login_db.php. I can't understand, how to initiate? – bhuvana Jul 25 '19 at 07:38
  • Yes, this function take the parameter `$email` and you give it in `index` but you give something that is never user. ` 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'); //$email should be initiate here $is_exist = $this->Login_db->isEmailExist($email); $this->load->view('bootstrap/footer'); } ` – GrenierJ Jul 25 '19 at 07:40
0

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

Swarna Sekhar Dhar
  • 550
  • 1
  • 8
  • 25
  • $this->form_validation->set_rules( 'username', 'Username', 'required|valid_email|is_unique[users.username]', array( 'is_unique' => 'Email already exists' ) ); – Swarna Sekhar Dhar Jul 25 '19 at 12:25
  • I tried this, but its not showing the message. public function index() { $this->load->view('bootstrap/header'); $this->load->library('form_validation'); $this->load->view('login_form'); $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[login.email]', array('is_unique' => 'Email already exists')); $this->form_validation->set_rules('password', 'Password', 'required'); $this->load->view('bootstrap/footer'); } – bhuvana Jul 26 '19 at 05:03