简体   繁体   中英

Login with Ajax in Codeigniter (Controllers and Models can not execute Query Database)

I have a View that will send Login data with Ajax jquery as follows : views/Login.php

 I have a View that will send Login data with Ajax jquery as follows : views/login.php <!-- begin snippet: js hide: false console: true babel: false --> 

That will send username & password to Model Login / insert_login controller/Login.php

 <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { public function __construct () { parent::__construct(); $this->load->model('Login_Model'); } public function index () { $this->load->view('Login'); } public function insert_login () { $user = $this->input->post('user'); $pass = $this->input->post('pass'); $cekUser = $this->Login_Model->check_user($user); if (empty($cekUser->username)) { $sts = 'user_wrong'; }else{ if (md5($pass) != $cekUser->password) { $sts = 'pass_wrong'; }else{ if ($cekUser->tipe_user == 'admin'){ $sts = 'success_admin'; $this->session->set_userdata(array('user'=>$user,'status'=>'admin')); redirect(base_url('Admin/Home')); }else{ $sts = 'success_user'; $this->session->set_userdata(array('user'=>$user,'status'=>'user')); redirect(base_url('Admin/Home')); } } } echo '{"status":"'.$user.'"}'; } public function logout () { $this->session->sess_destroy(); redirect(base_url('Admin/Login')); } } ?> <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login_Model extends CI_Model { public function check_user ($user) { return $this->db->get_where('m_user', array('username' => $user))->row(); } } ?> 

I wonder why Query Database Function get_where / query / Any query can not be executed, but I have added Liblary Database in autoload.php: $autoload['libraries'] = array('database','session'), And I test using a Controller other than Login Controller still can run. Why only in the model & Login controller can not execute Query?

But if I try to display the data captured divariabel:

  $user = $this->input->post('user'); $pass = $this->input->post('pass'); echo $user.' '.pass; 

When i test like it can already get and display data, tapi masalahnya kenapa ketika memunculkan Query tidak muncul apapun. Please help me, I've really stack, stay up every day to clean up my job ...

set data into array in controller file

set name of array key as your database field name username & password

controller file

  $user = $this->input->post('user');
  $pass = $this->input->post('pass');
  $where=array('username'=>$user,
            'password'=>md5($pass));
  $cekUser = $this->Login_Model->check_user($where);

give yout table name in from query

model file

class Login_Model extends CI_Model {

  public function check_user ($where)
  {
    $this->db->select('*');
    $this->db->from('table_name');
    $this->db->where($where);
    $res=$this->db->get();
    return $res->result();
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM