I have a problem with a form. I need to do a login form, but I can't retrieve login data.
This is my view login.php:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="main">
<div class="container">
<div class="card card-container">
<h1>Control de Asistencia</h1>
<form id="login" class="form-signin" method="post" action="<?php echo
base_url('login/ingresar'); ?>">
<span id="reauth-email" class="reauth-email"></span>
<input type="email" name="email" id="inputEmail" class="form-control"
placeholder="Usuario" required autofocus>
<input type="password" name="password" id="inputPassword" class="form-control"
placeholder="Clave" required>
<br/>
<button class="center btn btn-raised btn-info btn-lg" type="submit">Ingresar</button>
<div class="login__rpta"></div>
</form><!-- /form -->
</div><!-- /card-container -->
</div><!-- /container -->
</div><!--main-->
The 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');
}
function ingresar(){
$email = $this->input->post("email");
$password= md5($this->input->post("password"));
$resp = $this->Login_model->login($email, $password);
if($resp){
$data = [
"id" => $resp->id,
"name" => $resp->nombre,
"nivel" => $resp->nivel,
"login" => TRUE
];
$this->session->set_userdata($data);
}
else{
echo "errorxxx";
}
}
function cerrar(){
$this->session->sess_destroy();
redirect(site_url());
}
}
Javascript custom.js:
$(function () {
$.material.init();
$("#login").submit(function(event){
event.preventDefault();
$.ajax({
url:$(this).attr("action"),
type:$(this).attr("method"),
data:$(this).serialize(),
success:function(resp){
if(resp==="error"){
$('.login__rpta').html("Usuario o clave incorrectos");
console.log(resp);
}
else{
window.location.href = base_url + "welcome/home";
console.log(resp);
}
}
});
});
------ more code ------
});
EDITED: this is the model Login_model.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Login_model extends CI_Model {
function login($email, $password){
$this->load->database();
$this->load->dbutil();
// check connection details
if( !$this->dbutil->database_exists('myDatabase')) {
echo 'Not connected to a database, or database not exists';
} else {
echo 'Conectado';
}
$this->db->where("email", $email);
$this->db->where("password", $password);
$resultados = $this->db->get("profesores");
if ($resultados->num_rows()>0) {
return $resultados->row();
}
else{
return false;
}
}
}
The console.log from custom.js show me the view (the form) instead of the login parameters, and it redirects me to a 404 error page... What am I doing wrong, please? This is not my code (I'm just trying to fix it) and I can't see the problem!
window.location.href = base_url + "welcome/home";
which value does it hold, is it defined?