2016-02-23 100 views
0

嗨我正在創建一個使用ajax的CodeIgniter登錄表單,但它不能正常工作,因爲我想要的。它正在通過ajax重定向到我正在執行的頁面,但不執行do_login函數。如果我點擊登錄而不輸入任何字段,它不會顯示所需字段,而我在do_login函數中給出了驗證碼。請提出適當的解決方案,以解決如何使用這些函數通過ajax重定向的問題。 這是我的控制器user.php的CodeIgniter使用Ajax登錄表單

 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> 

這是我的模型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函數來驗證輸入。

回答

0

這裏是你的問題

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 
    } 
+0

謝謝妳,解決了一個問題,但我怎麼可以把它重定向然後 – Pittz

+0

歡迎你如果相應的網頁您認爲它可以幫助您取悅並標記答案爲正確。 重定向你必須在你的成功功能在ajax window.location.replace(「http://stackoverflow.com」); //你的鏈接在那裏 – Drudge

0

我建議你使用回聲json_encode($的數據);在你的控制器上,其中$ data是一個包含你的require字段或任何提示符的關聯數組。

在你的Ajax處理與回調 -

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

如果你不熟悉這個,你想你登錄系統來推動這個事情的AJAX,我建議你檢查出的javascript DOM,回調和其他相關的東西。

0

在您的控制器

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>