2016-03-10 65 views
0

在重定向部分,爲什麼它沒有去那個頁面。發生的是它得到Home/中的文字。JQuery登錄表單codeigniter

在頁面重定向期間,它不會進入頁面。相反,它會從Home/的文本,我收到此錯誤:

Uncaught Type Error : Cannot read property location of null

這裏是Controller代碼:

public function loginacc() 
{ 
    if(!$this->input->is_ajax_request()){ exit('no valid req.'); } 
    $FormRules = array(
      array(
        'field' => 'email', 
        'label' =>'Email', 
        'rules'=>'required|valid_email|trim|xss_clean' 
       ) 
     ); 
    $this->form_validation->set_rules($FormRules); 
    if($this->form_validation->run()==TRUE) 
    { 
     $email = trim(strip_tags($this->input->post('email'))); 
     $pass=trim(strip_tags($this->input->post('password'))); 
     $users = $this->db->get_where('accounts',array("email"=>$email,"pass"=>$pass))->result(); 
     if(empty($users)) 
     { 
      echo "failed"; 
     } 
     else 
     { 
      redirect(base_url().'Home/'); 
     } 
    }//form validation 
    else 
    { 
     echo '<div class="error">'.validation_errors().'</div>'; 
    } 
} 

這是我的腳本:

<script> 
     $(document).ready(function() 
     { 
      $("form.loginform").on('submit', function(form) 
      { 
       form.preventDefault(); 
       $.post('<?php echo base_url()?>Hauth/loginacc', $('form.loginform').serialize(),function(data) 
       { 
        $('#error').html(data); 
       }) 
      }); 
     }); 
    </script> 

這是我的登錄形式:

 <form action="" method="post" id="loginform" class="loginform" autocomplete="off" autofocus> 
      <div class="form-group has-feedback"> 
      <input type="email" class="form-control" placeholder="Email" name="email" id="lemail" > 
      <span class="glyphicon glyphicon-envelope form-control-feedback"></span> 
      </div> 
      <div class="form-group has-feedback"> 
      <input type="password" class="form-control" name="password" placeholder="Password" id="lpass" > 
      <span class="glyphicon glyphicon-lock form-control-feedback"></span> 
      </div> 
      <div class="row"> 
      <div class="col-xs-12" class="error" id="error" style="color:black;"></div> 
      <div class="col-xs-8"> 
       <div class="checkbox icheck"> 
       <label> 
        <input type="checkbox"> Remember Me 
       </label> 
       </div> 
      </div><!-- /.col --> 
      <div class="col-xs-4"> 
       <button type="submit" class="btn btn-primary btn-block btn-flat" id="btnlogin">Sign In</button> 
      </div><!-- /.col --> 
      </div> 
     </form> 

這是主頁視圖:

<html> 
<head> 
<title>LinkedShop</title> 
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css"> 
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'> 
<script type="text/javascript"> 

     window.close(); 
     window.opener.location.reload(); 

</script> 
</head> 

<body> 
    asdasdasdas 
</body> 
</html> 
+0

未捕獲的類型錯誤:無法讀取空 的財產位置由window.close引起的(); window.opener.location.reload(); 。已經修復它。現在的問題是,爲什麼它沒有重定向到我想要的控制器 –

+0

要清楚你想要做什麼,更重要的是,你得到你想要解決的錯誤。後端?前端?代碼行? –

+0

在控制檯中..後端 –

回答

0

我認爲這裏的整個問題是,你不能在PHP簡單地重定向一個ajax後門柱 - 試試這個,而不是

public function loginacc() 
{ 
    $arrReturnData = array(); 

    if($this->input->is_ajax_request()) 
    { 
     $FormRules = array(
       array(
         'field' => 'email', 
         'label' =>'Email', 
         'rules'=>'required|valid_email|trim|xss_clean' 
        ) 
      ); 
     $this->form_validation->set_rules($FormRules); 
     if($this->form_validation->run()) 
     { 
      $email = trim(strip_tags($this->input->post('email'))); 
      $pass=trim(strip_tags($this->input->post('password'))); 
      $users = $this->db->get_where('accounts',array("email"=>$email,"pass"=>$pass))->result(); 
      if(empty($users)) 
      { 
       $arrReturnData['error'] = "failed"; 
      } 
      else 
      { 
       $arrReturnData['success'] = base_url().'Home/'; 
      } 
     }//form validation 
     else 
     { 
      $arrReturnData['error'] = validation_errors(); 
     } 

    } 
    else 
    { 
     $arrReturnData['error'] = "no valid request"; 
    } 

    echo json_encode($arrReturnData); 

} 

和你的jQuery應該看起來像

$(document).ready(function() 
{ 
    $("form.loginform").on('submit', function(form) 
    { 
     form.preventDefault(); 
     $.post('<?php echo base_url()?>Hauth/loginacc', $('form.loginform').serialize(),function(data) 
     { 
      if (typeof data.success !== "undefined") 
      { 
       window.location.href = data.success; 
      } 
      else 
      { 
       $('#error').html(data.error); 
      } 

     }) 
    }); 
});