2017-02-13 2 views
1

以下のコントローラコードはログインできません。エラーの どれも問題が何であるかを私は、コントローラにポスト値を渡すためにbelow.howとしてajax.scriptを通じてポスト値を送信示す表示ページとしてコードの下にはアヤックスユーザー名とパスワードはajaxを使用してログインします。ログインできません

 public function loginn() 
      { 
       $email=$this->input->post('email'); 
       $password=$this->input->post('password'); 

       //$this->load->library('form_validation'); 

       $this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]'); 
       $this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[40]'); 

       if ($this->form_validation->run() && $this->Login_model->login($email, $password)) { 
        $this->welcome(); 
       } 
       else { 
        $this->form_validation->set_message('check_database', 'Invalid username or password'); 

        $this->index(); 

         } 
      } 

を通じて呼び出す方法を理解することはできません取得しますajaxを使って

<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#fpassf").click(function(){ 
     //e.preventDefault(); 
     var email = $("#email").val(); 
     var password= $("#password").val(); 


     $.ajax({ 
      type: "POST", 
      url: "<?php echo base_url() ?>"+"Index.php/Login_cntrl/loginn", 
      data: {email:email,password:password}, 
      success:function(data) 
      { 
       alert('Successfully login'); 
      }, 
      error:function() 
      { 
       alert('fail'); 
      } 
     }); 
    }); 
}); 


以下 enter image description here

+0

問題でどのようなあなたのjqueryのコード – Shayan

+0

を入れて?エラー?問題?実際の行動と期待される行動詳細が不足しているため投票を閉じる –

+0

AJAXレスポンスとPHPエラーをチェックしてください。多分、それはあなたが何が問題かを理解するのを助けるでしょう。 – mi6crazyheart

答えて

0

に示すサインインの画像をクリックしてくださいに以下 enter image description here

に示した画像は、あなたがCodeIgniterのフレームワークを使用しているようです。したがって、私はあなたのAjax URL Pathに疑念を抱いています。

あなたは

url: "<?php echo base_url() ?>"+"Index.php/Login_cntrl/loginn", 

を与えている。しかしCodeIgniterのでは、それはあなたがスクリプトファイルからこのAJAXを呼び出していることを

url: "<?php echo base_url() ?>"+"/Login_cntrl/loginn", 

OR

url: "<?php echo base_url('Login_cntrl/loginn') ?>", 

ようにそれが可能にすることもできなければなりません。これはPHPファイルではありません。 base_url()関数はそこでは動作しません。その場合は、base_urlを入力変数に隠しフォーマットで保存する必要があります。あなたのAjaxコードで取得する必要があります。

<input type="hidden" name="myurl" value="<?php echo base_url();"> 

、その後、アヤックス1

var myurl = $("#myurl").val(); 
+0

申し訳ありませんあなたのコードは動作していません –

0

両方が異なる応答を送信するので、あなたは、あなたの基本的なログインアクションとAjaxログインアクションを分離する必要があります。

PHPコントローラー:

public function ajax_login() 
{ 
    $email = $this->input->post('email'); 
    $password = $this->input->post('password'); 

    $this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]'); 
    $this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[40]'); 

    if ($this->form_validation->run() && $this->Login_model->login($email, $password)) { 
     return $this 
      ->output 
      ->set_status_header(200) 
      // here you tell to the ajax that the login is successful 
      ->set_output(json_decode(array('status' => 'success', 'message' => 'PUT_YOUR_SUCCESS_MESSAGE_HERE'))) 
     ; 
    } else { 
     return $this 
      ->output 
      ->set_status_header(200) 
      // here you tell to the ajax that the login is failed 
      ->set_output(json_decode(array('status' => 'error', 'message' => 'PUT_YOUR_ERROR_MESSAGE_HERE'))) 
     ; 
    } 
} 

Javascriptを:

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#fpassf").click(function(){ 
      var email = $("#email").val(); 
      var password = $("#password").val(); 

     $.ajax({ 
      type: "POST", 
      url: "<?php echo base_url('Login_cntrl/ajax_login') ?>", 
      data: {email:email, password:password}, 
      success: function(data) { 
       data = JSON.parse(data); 

       // success in ajax does not mean successfully login, so check again 
       if (data.status == 'success') { 
        alert(data.message]); // login success message defined in action 
       } else { 
        alert(data.message); // login failed message defined in action 
       } 
      }, 
      error: function() { 
        // error in ajax means HTTP error, not login error 
        alert('Request failed!'); 
       } 
      }); 
     }); 
    }); 
</script> 
関連する問題