Php 使用Ajax的CodeIgniter登录表单

Php 使用Ajax的CodeIgniter登录表单,php,ajax,codeigniter,Php,Ajax,Codeigniter,您好,我正在使用ajax使用CodeIgniter创建一个登录表单,但它并没有像我所希望的那样正常工作。它被重定向到我正在通过ajax执行的页面,但没有执行do_登录功能。如果我在没有输入任何字段的情况下单击login,则不会显示必填字段,而我已在do_login函数中给出了验证代码。对于如何使用这些函数通过ajax重定向的问题,请为我提供适当的解决方案。 这是我的controller user.php class User extends CI_Controller { publ

您好,我正在使用ajax使用CodeIgniter创建一个登录表单,但它并没有像我所希望的那样正常工作。它被重定向到我正在通过ajax执行的页面,但没有执行do_登录功能。如果我在没有输入任何字段的情况下单击login,则不会显示必填字段,而我已在do_login函数中给出了验证代码。对于如何使用这些函数通过ajax重定向的问题,请为我提供适当的解决方案。 这是我的controller user.php

     class User extends CI_Controller {


public function index()
{
    $this->load->view('login');
}



public function do_login(){
    $this->form_validation->set_rules('username','Username','required');
    $this->form_validation->set_rules('password','Password','required|callback_verifyUser');
if($this->form_validation->run()== false){
    echo "sorry";                                     // where to put this echo to make it work through ajax
    $this->load->view('login');
}
else{
    echo "loggedIn";                                  // where to put this echo to make it work through ajax
    $data = array(
    'username' => $this->input->post('username'),
    'is_logged_in'=>1
    );
    $this->session->set_userdata($data);

    redirect('user/members');

}
}
public function members()
{
    if($this->session->userdata('is_logged_in')){

    $this->load->view('home');
    }
    else{
        redirect('user/restricted');
    }
}
public function restricted()
{
    $this->load->view('restricted');
}

public function verifyUser(){
    $username=$this->input->post('username');
    $password= $this->input->post('password');

    $this->load->model('LoginModel');
    if($this->LoginModel->login($username, $password)){
        return true;
    }
    else{
        $this->form_validation->set_message('verifyUser','Invalid Email or Password: Please enter it correctly');
        return false;
    }
}
public function logout(){
    $this->session->sess_destroy();
    redirect('user/index');
}


    }
    ?>
这是我的视图文件login.php

    <html>
    <head>
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="http://127.0.0.1/simple_login_comp/js/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#frm_login").submit(function(event){
        event.preventDefault();

$.ajax({
url: "http://127.0.0.1/simple_login_comp/index.php/user/do_login",  
type: "POST",
data: 
{
    username: $('#username').val(),
    password: $('#password').val()},
success: function(data) 
{
    if (data== 'loggedIn')
    {   
        alert("you are logged IN");
        //window.location.replace("http://127.0.0.1/simple_login_redirect/index.php/user/home");
        //window.location.href="http://127.0.0.1/simple_login_comp/index.php/user/members";
    }
    else if(data== 'sorry'){

        alert("sorry");
    }
    //else{
    //  alert(data);
    //}
}
    });
});
});
</script>
</head>
<body>
<div class="container">
        <div class="row">
            <div class="span12">
                <div class="well">
                    <center><h1>Be a member of Mrwebsiter</h1></center>
                </div>
            </div>
        </div>
<h1>Login</h1>
<p>Please fill your details to login</p>

<form action="" id="frm_login" method="post">
Username :</br>
<input type="text" name="username" id="username"/></br>
Password :</br>
<input type="password" name="password" id="password"/></br>

<input type="submit" value="Login" name ="submit"/>
</form>
<div class="row">
    <div class="span12">
        <div class="well">
            <center><h3>copyright</h3></center>
        </div>
    </div>
</div>
</div>
</body>
</html>

$(文档).ready(函数(){
$(“#frm_登录”).submit(函数(事件){
event.preventDefault();
$.ajax({
url:“http://127.0.0.1/simple_login_comp/index.php/user/do_login",  
类型:“POST”,
数据:
{
用户名:$('#用户名').val(),
密码:$('#password').val()},
成功:功能(数据)
{
如果(数据=='loggedIn')
{   
警报(“您已登录”);
//window.location.replace(“http://127.0.0.1/simple_login_redirect/index.php/user/home");
//window.location.href=”http://127.0.0.1/simple_login_comp/index.php/user/members";
}
否则,如果(数据==‘对不起’){
警惕(“抱歉”);
}
//否则{
//警报(数据);
//}
}
});
});
});
成为Mrwebsiter的成员
登录
请填写您的详细信息以登录

用户名:

密码:

版权
这是我的模型LoginModel.php

     <?php


    class LoginModel extends CI_Model {


public function login($username, $password)
{
    $this->db->select('username', 'password');
    $this->db->from('users');
    $this->db->where('username', $username);
    $this->db->where('password', $password);

    $query= $this->db->get();

    if($query->num_rows() == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}



}
?>


我的登录表单不起作用,有人可以建议我如何运行它,以及我在哪里犯了错误。当我通过ajax重定向它时,它不会通过do_login函数验证输入,它会直接重定向到页面。

这是您的问题。

 if($this->form_validation->run()== false){
        echo "sorry";                                     // where to put this echo to make it work through ajax
        $this->load->view('login');
    }
将此更改为

if($this->form_validation->run()== false){
     echo "sorry";exit
    }

我建议您使用echo json_encode($data)在控制器上,其中,$data是一个关联数组,包含所需字段或任何提示

在ajax上,使用-

success: function(data) {
   var response = eval('(' + data + ')');
   // DOM codes here
} // rest of the codes here

如果您不熟悉这一点,并且希望在登录系统上推广ajax,我建议您查看javascript dom、回调和其他相关内容。

在控制器上

public function do_login(){
    $this->form_validation->set_rules('username','Username','required');
    $this->form_validation->set_rules('password','Password','required|callback_verifyUser');
    if($this->form_validation->run()== false){
        echo "sorry";                                     // where to put this echo to make it work through ajax

    }
    else{

            $data = array(
             'username' => $this->input->post('username'),
             'is_logged_in'=>1
             );
            $this->session->set_userdata($data);
            echo "loggedIn";         // where to put this echo to make it work through ajax

     }
 }
你的观点

<script type="text/javascript">
$(document).ready(function(){
$("#frm_login").submit(function(event){
    event.preventDefault();

$.ajax({
url: "http://127.0.0.1/simple_login_comp/index.php/user/do_login",  
type: "POST",
data: 
{
    username: $('#username').val(),
    password: $('#password').val()},
    success: function(data) 
    {
        if (data== 'loggedIn')
        {   
            alert("you are logged IN");
            window.location ="http://127.0.0.1/simple_login_redirect/index.php/user/members");

        }
        else if(data== 'sorry'){

            alert("sorry");
        }

    }
    });
});
});
</script>

$(文档).ready(函数(){
$(“#frm_登录”).submit(函数(事件){
event.preventDefault();
$.ajax({
url:“http://127.0.0.1/simple_login_comp/index.php/user/do_login",  
类型:“POST”,
数据:
{
用户名:$('#用户名').val(),
密码:$('#password').val()},
成功:功能(数据)
{
如果(数据=='loggedIn')
{   
警报(“您已登录”);
window.location=”http://127.0.0.1/simple_login_redirect/index.php/user/members");
}
否则,如果(数据==‘对不起’){
警惕(“抱歉”);
}
}
});
});
});

谢谢,它解决了一个问题,但我如何将其重定向到相应的页面如果您认为这有助于您,欢迎您投票并将答案标记为正确。要重定向,您必须在ajax窗口的success函数中执行。location.replace(“);//此处的链接