[英]In codeigniter How to redirect after login using ajax
我有一个登录弹出模态。 我正在通过ajax登录
模态
<div class="modal-body">
<form action="<?php echo base_url('Login');?>" method="POST">
<div class="form-group">
<input type="text" placeholder="Email or Mobile*" value="" id="loginEmail" name="email" class="form-control input-feild">
</div>
<div class="form-group">
<input type="password" placeholder="Password*" value="" id="loginPassword" name="password" class="form-control input-feild">
</div>
<div class="form-group">
<input type="button" id="l_submit" name="l_submit" value="Login" class="btn btn-primary input-feild">
</div>
</form>
<p id="error-msg"></p>
</div>
我尝试使用ajax成功登录后进行重定向。 如果电子邮件和密码正确,则重定向到任何页面。 如果没有,它将显示错误。
控制者
function index() {
$this->form_validation->set_rules('email', 'Email', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == false) {
echo validation_errors();
}else {
$email = $this->input->post("email");
$password = $this->input->post("password");
$user = $this->Perfect_mdl->check_user($email, $password);
if ($user) {
$logged_in_data = array();
foreach ($user as $logged_in_data) {
$logged_in_data = array(
'id' => $user[0]->id,
'email' => $user[0]->email
);
}
$this->session->set_userdata($logged_in_data);
$id = $this->session->userdata('email');
$data['details'] = $this->Perfect_mdl->get_login_user_detail($id);
echo "Yes";
}else {
echo "No";
}
}
}
这是我正在检查登录用户电子邮件和密码正确/不正确的控制器。
这是我的剧本
<script type="text/javascript">
$("#l_submit").click(function(){
var email = $("#loginEmail").val();
var password = $("#loginPassword").val();
$.ajax({
url : "<?php echo base_url('Login');?>",
type: 'POST',
data : {'email':email,'password':password},
success: function(msg) {
if (msg == "Yes")
window.location.href = "<?php echo current_url(); ?>";
else if (msg == "No")
$('#error-msg').html('<div class="alert alert-danger text-center">Incorrect Email & Password. Please try again ...</div>');
else
$('#error-msg').html('<div class="alert alert-danger">' + msg + '</div>');
}
});
return false;
});
</script>
正如您在成功部分中看到的那样,当我输入正确的电子邮件/密码时。 它显示是。 但是我想重定向另一个页面,为什么在正确的电子邮件/密码上显示YES ,而在错误的电子邮件/密码上显示NO 。
我在哪里做错了???
您需要在jQuery和Controller函数中更改几行代码。 在这里,我附上您代码的更新版本。 请参考以下内容:
视图(引导程序模式)
<div class="modal-body">
<form action="<?php echo base_url('Login');?>" method="POST">
<div class="form-group">
<input type="text" placeholder="Email or Mobile*" value="" id="loginEmail" name="email" class="form-control input-feild">
</div>
<div class="form-group">
<input type="password" placeholder="Password*" value="" id="loginPassword" name="password" class="form-control input-feild">
</div>
<div class="form-group">
<input type="button" id="l_submit" name="l_submit" value="Login" class="btn btn-primary input-feild">
</div>
</form>
<p id="error-msg"></p>
</div>
这是您的视图文件。 它将保持不变。 单击按钮后,您编写了一个脚本,需要对其进行修改。 附加控制器功能后将起作用:
控制者
function index() {
if (!$this->input->is_ajax_request()) {
echo 'No direct script is allowed';
die;
}
$this->form_validation->set_rules('email', 'Email', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == false) {
$result['status'] = 'error';
$result['message'] = validation_errors();
}else {
$email = $this->input->post("email");
$password = $this->input->post("password");
$user = $this->Perfect_mdl->check_user($email, $password);
if ($user) {
$logged_in_data = array();
foreach ($user as $logged_in_data) {
$logged_in_data = array(
'id' => $user[0]->id,
'email' => $user[0]->email
);
}
$this->session->set_userdata($logged_in_data);
$id = $this->session->userdata('email');
$data['details'] = $this->Perfect_mdl->get_login_user_detail($id);
$result['status'] = 'success';
$result['message'] = 'Yeah! You have successfully logged in.';
$result['redirect_url'] = base_url();
}else {
$result['status'] = 'error';
$result['message'] = 'Whoops! Incorrect Email & Password. Please try again';
}
}
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($result));
$string = $this->output->get_output();
echo $string;
exit();
}
脚本
<script type="text/javascript">
$("#l_submit").click(function(){
var email = $("#loginEmail").val();
var password = $("#loginPassword").val();
$.ajax({
url : "<?php echo base_url('Login');?>",
type: 'POST',
data : {'email':email,'password':password},
success: function(resp) {
if (resp.status == "success")
window.location.href = resp.redirect_url;
else
$('#error-msg').html('<div class="alert alert-danger">' + resp.message + '</div>');
}
});
return false;
});
这是正确的答案。 让我知道您是否遇到任何问题。
如果要从脚本跳转到另一个控制器/方法,则可以在代码中使用简单的redirect()
(位于url帮助器中)。 但是,这不会提醒用户登录成功:它只是普通的重定向。
正确的方法是返回一个json文件,因此不要回显yes或no,请使用以下命令:
$response['type'] = 'error';
$response['msg'] = 'Login Succesful';
$response['redirect'] = 'http://your-url.com/controller/method';
echo json_encode($response);
die;
然后,您在ajax调用中处理答案,并发出一些良好的警报(在我的情况下为Sweet Alert),如下所示:
success: function(data) {
if (data.redirect) {
swal({
title: '¡Success!',
text: data.msg,
timer: 2000,
type: data.type,
showConfirmButton: false
}, function() {
window.location.href = data.redirect;
});
} else {
swal('¡Error!', data.msg, data.type);
}
}
因此,这段代码的作用是,如果您的json响应具有重定向字段,则将在一段时间后触发重定向。 但是,如果您的json响应没有字段重定向,则只会显示警报。
检查您的返回数据类型。 可能是您可以在Ajax代码中的数据类型中使用。 如果有条件,则返回数据无法读取。 我面临相同的问题或使用此
if($。trim(msg)=='是'){
更改echo "Yes";
die("Yes");
。 这将确保在“是”或“否”之后什么都不会发送,然后JS代码将按预期工作。
成功后,它们也是从控制器重定向的一种方法。 在这种情况下,您只需要按照以下两个步骤进行重定向即可:在控制器中的此行,当您遇到真实情况并想要重定向时
if($result){
echo base_url()."welcome/";
}
else{
echo 0;
}
在您应用了ajax的html页面上,在真实情况下,您可以尝试重定向页面
success: function(result){
if(result!=0){
// On success redirect.
window.location.replace(result);
}
else
alert('wrong');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.