gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:25:23 24 4
gpt4 key购买 nike

我的表单有问题。我需要制作登录表单,但无法检索登录数据。

这是我的 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-->

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 自定义.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 ------
});

已编辑:这是模型 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;
}
}
}

custom.js 中的 console.log 向我显示 View (表单)而不是登录参数,并将我重定向到 404 错误页面...请问我做错了什么?这不是我的代码(我只是想修复它),我看不到问题!

最佳答案

你的base_url在codeigniter的config.php中定义,例如:

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

这并不意味着您可以简单地使用 base_url 作为 JavaScript 中的变量来希望获得该值。

但是你可以像这样回显它:

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

或者只使用相对路径

window.location.href = '/welcome/home'

关于javascript - 从表单检索数据(Codeigniter、AJAX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59179364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com