-2

I received an error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: controllers/c_verifylogin.php

Line Number: 17

This is the filename that has an error:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_verifyLogin extends CI_Controller {
    function __construct() {
        parent::__construct();
        //load session and connect to database
        $this->load->model('m_login','login',TRUE);
        $this->load->helper(array('form', 'url','html'));
        $this->load->library(array('form_validation','session'));
    }

    function index() {
        $this->form_validation->set_rules('studentid', 'studentid', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');

        if($this->form_validation->run() == FALSE) {
            $this->load->view('v_login');
            } else {
                //Go to private area
                redirect(base_url('c_home'), 'refresh');
            }       
     }

     function check_database($password) {
         //Field validation succeeded.  Validate against database
         $studentid = $this->input->post('studentid');
         //query the database
         $result = $this->login->login($studentid, $password);
         if($result) {
             $sess_array = array();
             foreach($result as $row) {
                 //create the session
                 $sess_array = array('studentid' => $row->studentid);
                 //set session with value from database
                 $this->session->set_userdata('logged_in', $sess_array);
                 }
          return TRUE;
          } else {
              //if form validate false
              $this->form_validation->set_message('check_database', 'Invalid username or password');
              return FALSE;
          }
      }
}
/* End of file c_verifylogin.php */
/* Location: ./application/controllers/c_verifylogin.php */

and this is the view which the data is passed

<!DOCTYPE html>
<html>
 <head>
   <title>Simple Login with CodeIgniter</title>
 </head>
 <body>
   <h1>Simple Login with CodeIgniter</h1>
   <?php echo validation_errors(); ?>
   <?php echo form_open('c_verifylogin/index'); 
   echo form_label("StudentID: ");
   echo form_input("studentid");
   echo br();
   echo form_label("Password: ");
   echo form_password("password");
   echo br();
   echo form_submit("","Login");
   echo form_close();
   ?>
 </body>
</html>

I've been searching for the problem for hours, and I still don't know what's wrong with it. Can someone help?

Natty Guurl
  • 125
  • 1
  • 3
  • 18

3 Answers3

1

$data variable is not defined here..define it with some value of if you want to pass it else don't pass $data to view call...

    if($this->form_validation->run() == FALSE) {
        $this->load->view('v_login',$data);
        } else {
            //Go to private area
            redirect(base_url('c_home'), 'refresh');
        }       
 }
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
1

Either you have to define $data

 $data = array('something');
 $this->load->view('v_login',$data);

or

remove $data

$this->load->view('v_login');
AVM
  • 592
  • 2
  • 11
  • 25
1

Here in your code clearly show that you didn't define $data variable.
So you have 2 options either define $data as array like

$data = array(); 

or simply load the view without passing any variable like

$this->load->view('v_login');

I hope it will help you.

oldlearner
  • 205
  • 1
  • 4
Robin Garg
  • 203
  • 2
  • 13