1
votes

I am new to PHP Codeigniter framework. I got this error in my code and i am unable to consider the error.

Using the login validation and flashdata(message) but not getting proper output having error in my code.

view:-

<div class="row justify-content-center"> 
  <div class="col-6">
        <h1><?php echo $title ?></h1>
        <?php if($this->session->flashdata('message')) { ?>
          <div class="alert alert-danger"><?php echo $this->session->flashdata('message')?></div>
        <?php }?>
        <?php echo form_open('user/login', array('id' => 'loginForm') ) ?>
          <div class="form-group" >
            <input type="text" name="email" id="email" class="form-control" placeholder="email" />
            <?php echo form_error('email','<div class="error">','</div>') ?>
          </div>
          <div class="form-group" >
            <input type="password" name="password" id="password" class="form-control" placeholder="password" />
            <?php echo form_error('password','<div class="error">','</div>') ?>
          </div>
          <div class="form-group" >
            <input type="submit" name="submit" value="Login" class="btn btn-primary" />
          </div>
          <?php echo form_close(); ?>
  </div>

controller:-

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends CI_Controller {

function __construct() {
    parent::__construct();
    $this->load->library('form_validation');
    
} 

public function login()
{
    $data['title'] = "Login";

    
    $this->form_validation->set_rules('email','email','trim|required|valid_email');
    $this->form_validation->set_rules('password','password','required');
    
    
    $this->form_validation->set_error_delimiters('<div class="error">','</div>');

 if($this->form_validation->run() == false)
 {
    $this->load->view('admin/login', $data);
    $this->load->view('home/index',$data );
 }
 else{
     $email = $this->security->xss_clean($this->input->post('email'));
     $password = $this->security->xss_clean($this->input->post('password'));
     
     $admin = $this->admin_model->login($email,$password);

     if($admin){
         $userdata = array(
             'id' => $admin->id,
             'first_name' => $admin->first_name,
             'last_name' => $admin->last_name,
             'authenticated' => TRUE

         );

         $this->session->set_userdata($userdata);

         redirect('dashboard');
    
     }
     else{
         $this->session->set_flashdata('message','Invalid email or Password');
         redirect('admin/login');
     }
 }
}
public function logout()
{
    $this->session->sess_destroy();
    redirect('admin/login');
}

}

1
What error are you getting, here it seems you are sending form data to the user Controller while your login codes are in Admin Controller.Sayd Fuad
You need to load (or autoload) the form helper to enable form_open() and form_close()Javier Larroulet

1 Answers

2
votes

You have to load form helper in Autoload.php like $autoload['helper'] = array('form');, And in form action you have mentioned user/login path, but below you have loaded Admin Controller.