简体   繁体   English

从表单中检索数据(Codeigniter、AJAX)

[英]Retrieving data from a form (Codeigniter, AJAX)

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:这是我的观点 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:控制器 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: 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编辑:这是模型 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? custom.js 中的 console.log 向我显示视图(表单)而不是登录参数,并将我重定向到 404 错误页面...请问我做错了什么? This is not my code (I'm just trying to fix it) and I can't see the problem!这不是我的代码(我只是想修复它),我看不到问题!

Your base_url is defined in codeigniter's config.php, for example like:您的 base_url 在 codeigniter 的 config.php 中定义,例如:

$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.这并不意味着您可以简单地使用 base_url 作为 javascript 中的变量,希望获得该值。

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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