在登录尝试失败后是否可以使codeigniter不重定向并在同一登录框中显示消息。
我的登录框是一个弹出窗口类型,在我点击登录按钮后显示,并在点击背景后消失。
控制器
public function login_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required|trim|callback_validate_credentials');
$this->form_validation->set_rules('password','Password','required|md5');
if($this->form_validation->run()){
$data=array(
'email' => $this->input->post('email'),
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('site/members');
}else {
$this->main();
}
}
public function validate_credentials(){
$this->load->model('model_users');
if($this->model_users->can_log_in()){
echo 'success';
} else {
echo 'failed';
}
}
我正在尝试使用错误消息更改elses,但它仍然会重定向到另一个页面。
HTML:
<div id="login-box" class="login-popup">
<input id="username" name="email" value="" type="text" autocomplete="on" placeholder="Username">
<input id="password" name="password" value="" type="password" placeholder="Password">
<button name="login_submit" class="submitbutton" onclick="checkFormData()">Login</button>
</div>
<script>
function checkFormData(){
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>site/validate_credentials",
data: {
email: $("#email").val(),
password: $("#password").val()
},
success: function(response) {
if(response == 'success'){
location.href = 'site/members';
} else {
$("#errors").show();
}
}
});
}
</script>
答案 0 :(得分:0)
如果您想在模态中使用警报,则需要使用ajax(javascript或jquery)检查数据。例如,你有这样的东西:
function checkFormData(){
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>your_controller/validate_credentials",
data: {
email: $("#email").val(),
password: $("#password).val()
},
success: function(response) {
if(response == 'success'){
location.href = <where you want to send them>;
} else {
$("#errors").show();
}
}
});
}
您的HTML看起来像:
<div class="hide" id="errors">Incorrect username/password.</div>
<input type="email" id="email" />
<input type="password" id="password" />
class =“hide”意味着与CSS相关的东西,你会有像“display:none;”这样的东西。 然后在你的控制器中你会这样检查:
if($this->model_users->can_log_in()){
echo 'success';
} else {
echo 'failed';
}
回显的响应将发送到您的ajax成功函数。
编辑:
您无需使用上述表单提交工作,因为使用该方法我们不想提交表单,我们希望从中传递数据。
<div id="login-box" class="login-popup">
<input id="username" name="email" value="" type="text" autocomplete="on" placeholder="Username">
<input id="password" name="password" value="" type="password" placeholder="Password">
<button name="login_submit" class="submitbutton" onclick="checkFormData()">Login</button>
</div>
我还将上面的javascript函数更改为函数。
答案 1 :(得分:0)
使用
window.location.href = '/redirectpage'