所以我想在登录过程成功后用jquery重定向到另一个控制器。 而是页面只重定向到自己。 香港专业教育学院尝试了jquery的所有3种可能性,它唯一无法重定向到另一个控制器。
进程可能:表单[post] - > controller->返回jquery->重定向到仪表板页面
https://file.town/download/e2dhyf2yyabpn6kuh79q3dylo如果您想尝试,请输入源代码。
这是view.php
<?php include('Header.php'); ?>
<div class="container">
<div class="row">
..........
<?php echo form_open('Login/check_login')?>
<div class="card">
<div class="card-content">
<div class="row">
<div class="input-field col s6 m12">
<i class="material-icons prefix">account_circle</i>
<input id="nim" type="text" class="validate" name="nim">
<label for="nim">NIM</label>
</div>
<div class="input-field col s6 m12">
<i class="material-icons prefix">visibility_off</i>
<input id="password" type="password" class="validate" name="password">
<label for="password">Password</label>
</div>
<div class="col s6 m12">
<button type="submit" value="login" class="waves-effect waves-light btn #1976d2 blue darken-2 submit1">LOGIN</button>
<button type="reset" value="cancel" class="waves-effect waves-light btn #1976d2 blue darken-2">CANCEL</button>
</div>
</div>
</div>
</div>
<?php echo form_close()?>
.......
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".submit1").click(function(event) {
event.preventDefault();
var nim = $("#nim").val();
var password = $("#password").val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>"+"Login/check_login",
dataType: 'json',
data: {nim:nim, password:password},
success: function(res) {
if(res.hasil == 'a'){
swal({
title: "Login sukses",
text: "Horeee",
showConfirmButton: true,
confirmButtonColor: '#0760ef',
type:"success"});
window.location.href = "<?php echo site_url('User');?>";
}else if(res.hasil == 'u'){
swal({
title: "Login sukses",
text: "Horeee",
showConfirmButton: true,
confirmButtonColor: '#0760ef',
type:"success"});
window.location.href = "<?php echo site_url('User');?>";
}else{
swal({
title: "Login Gagal",
text: "Pastikan NIM dan Password Benar",
showConfirmButton: true,
confirmButtonColor: '#0760ef',
type:"error"});
}
}
});
});
});
这是控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class login extends CI_Controller {
public function __construct()
{
parent::__construct();
// Load form helper library
$this->load->helper('form');
// Load form validation library
$this->load->library('form_validation');
// Load session library
$this->load->library('session');
// Load database model
$this->load->model('Mlogin');
}
public function index()
{
$this->load->view('login/Vlogin');
}
public function check_login()
{
$this->form_validation->set_rules('nim', 'NIM', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == FALSE ){
$this->load->view('login/Vlogin');
}
else{
$akun = array(
'nim' => $this->input->post('nim'),
'password' => md5($this->input->post('password'))
);
$result = $this->Mlogin->check_akun($akun);
if($result != false){
$result = $this->Mlogin->get_information($akun);
$session_data = array(
'nim' => $akun['nim'],
'nama' => $result[0]->nama,
'gambar'=> $result[0]->name,
'akses' => $result[0]->akses,
'status' => $result[0]->status,
'loggedin' => true,
);
$this->session->set_userdata('logged_in', $session_data);
if ($result[0]->akses=='a') {
$hasil['hasil'] = 'a';
echo json_encode($hasil);
}
else{
$hasil['hasil'] = 'u';
echo json_encode($hasil);
}
}
else
{
$hasil['hasil'] = 'false';
echo json_encode($hasil);
}
}
}
或者可能有其他选择?
答案 0 :(得分:0)
您的form
在您的ajax完成之前提交。
我建议您处理form submit
事件;出于调试目的,我已将警报替换为重定向。
$(document).ready(function() {
$("form").submit(function(event) {
event.preventDefault();
var nim = $("#nim").val();
var password = $("#password").val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>"+"Login/check_login",
dataType: 'json',
data: {nim:nim, password:password},
success: function(res) {
if(res.hasil == 'a'){
swal({
title: "Login sukses",
text: "Horeee",
showConfirmButton: true,
confirmButtonColor: '#0760ef',
type:"success"});
//window.location.href = "<?php echo site_url('User');?>";
alert(res.hasil);
}else if(res.hasil == 'u'){
swal({
title: "Login sukses",
text: "Horeee",
showConfirmButton: true,
confirmButtonColor: '#0760ef',
type:"success"});
//window.location.href = "<?php echo site_url('User');?>";
alert(res.hasil);
}else{
swal({
title: "Login Gagal",
text: "Pastikan NIM dan Password Benar",
showConfirmButton: true,
confirmButtonColor: '#0760ef',
type:"error"});
}
}
});
return false;
});
});