2

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!

4
  • in your js code, how is your variable base_url defined?
    – Vickel
    Commented Dec 4, 2019 at 15:42
  • It's in the config.php
    – Nira
    Commented Dec 4, 2019 at 15:46
  • ok, but in the line window.location.href = base_url + "welcome/home"; which value does it hold, is it defined?
    – Vickel
    Commented Dec 4, 2019 at 15:48
  • You're right... console.log(base_url) shows only https://
    – Nira
    Commented Dec 4, 2019 at 15:53

1 Answer 1

5

Your base_url is defined in codeigniter's config.php, for example like:

$config['base_url'] =  'http://or.org/';

That doesn't mean you can simply use base_url as a variable in javascript hoping to get that value.

But you can echo it out like:

window.location.href = '<?php echo base_url()?>' + 'welcome/home'

or just use the relative path

window.location.href = '/welcome/home'
4
  • It know redirect me to duplicated url: blabla.cccccc.com/blabla.ccccc.com/welcome/home
    – Nira
    Commented Dec 4, 2019 at 16:01
  • check your $config['base_url']
    – Vickel
    Commented Dec 4, 2019 at 16:05
  • My config.php has $allowed_domains = array('blabla.ccccc.com','blabla.cccccdea.edu'); $default_domain = 'blabla.ccccc.com'; if (in_array($_SERVER['HTTP_HOST'], $allowed_domains, TRUE)){ $config['base_url'] = 'https://'.$_SERVER['HTTP_HOST']; }else{ $config['base_url'] = 'https://'.$default_domain; }
    – Nira
    Commented Dec 4, 2019 at 16:10
  • I will open 2 different questions and be more specific, thanks.
    – Nira
    Commented Dec 4, 2019 at 16:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.