2017-06-12 6 views
1

私は現在航空会社システムを開発中です。私が行ったモジュールは次のとおりです:利用可能なフライトの検索、ログイン、登録。私の検索では、私が持っているフィールドは次のとおりです:2つのラジオボタン(片道と往復); 2ドロップダウンリスト(発信元と宛先)。 2 datepicker(あなたが往復を選択した場合、返却日と出発日)。片方向を選択した場合、戻りフィールドは非表示になります。AJAX codeigniterを使用して前の検索を取得する

これらのフィールドを入力すると、使用可能なすべてのフライトが表示され、「フライトを選択」ボタンが表示されますが、フライトを選択する前にログインする必要があることを確認します。

注:私がログインの検証のために行ったことは、まず両方のフィールド(ユーザー名とパスワード)に値があるかどうかを確認する必要があります。 2番目にユーザー名とパスワードが一致する場合は、そのアカウントのすべての基本情報が1つの変数に保存されます。第3に、アカウントが検証されているかどうかをチェックし、そうであれば、セッションを使用してすべての情報を保存します。

また、私はエコー "<スクリプト>"を試してみましたが、これは私のコントローラでプロンプトメッセージが表示される場所です。プロンプトメッセージを表示した後、利用可能なすべてのフライトは私に今は$ flight_from、$ flight_toなどのエラーを表示します。

例:

(O)片道(x)の往復(これを選んだ)

から:国1

へ:国2

出発:2017年6月22日

戻り値:7月19,2017

乗客数:2検索今

をクリックし、すべての利用可能なフライトは、私は選択のフライトをクリックすると、選択飛行ボタン

を持っているし、あなたはそれがリダイレクトされますログインする必要がありますメッセージプロンプトが存在します表示されますユーザ/検索に

しかし、問題は後に、それは私にエラー$のflight_fromを与えるリダイレクトされるなど

質問:それで質問は、プロンプトメッセージを表示した後、私は、ユーザーが検索すべての利用可能なフライトを表示したいれますAJAXを使用します。

コントローラ

//メソッドユーザーが選択飛行

public function search() 
{ 
    $data['countries'] = $this->CrudModel->get('countries'); 
    $flight_type = $_POST['flight_type']; 
    $pass_num = $_POST['no_of_passengers']; 
    $testing = $this->session->set_userdata('pass_num',$pass_num); 

    $this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert" style="padding:0px;" >', '</div>'); 

    $this->form_validation->set_rules('flight_from', 'Select depature', 'required|trim'); 
    $this->form_validation->set_rules('flight_to', 'Select Destination', 'required|trim'); 

    if ($flight_type == 'round_trip') 
    { 
     $this->form_validation->set_rules('depart', 'Date of flight', 'required|trim'); 
     $this->form_validation->set_rules('return', 'Date of return', 'required|trim'); 
     $this->form_validation->set_rules('no_of_passengers', 'Number of Passengers', 'required'); 
     if ($this->form_validation->run() == FALSE) 
     { 
      $this->index(); 

     } 
     else 
     { 
      $search_result = array(
       $flight_from = $_POST['flight_from'], 
       $flight_to = $_POST['flight_to'], 
       $depart = $_POST['depart'], 
       $return = $_POST['return'], 
       $no_of_passengers = $_POST['no_of_passengers'] 
      ); 
      $data['search_result'] = $this->CrudModel->search_round_trip('flight',$flight_from,$flight_to,$depart,$return,$no_of_passengers); 
      $this->session->tempdata($data['search_result']); 
      $this->session->tempdata(); 
      $this->load->view('partials/header'); 
      $this->load->view('partials/nav'); 
      $this->load->view('result',$data); 
     } 

    } 
    else 
    { 
     $this->form_validation->set_rules('depart', 'Date of flight', 'required|trim'); 
     $this->form_validation->set_rules('no_of_passengers', 'Number of Passengers', 'required'); 
     if ($this->form_validation->run() == FALSE) 
     { 
      $this->index(); 

     } 
     else { 
      $search_result = array(
       $flight_from = $_POST['flight_from'], 
       $flight_to = $_POST['flight_to'], 
       $depart = $_POST['depart'], 
       $no_of_passengers = $_POST['no_of_passengers'] 
      ); 
     $data['search_result'] = $this->CrudModel->search_one_way('flight',$flight_from,$flight_to,$depart,$no_of_passengers); 

     $this->load->view('partials/header'); 
     $this->load->view('partials/nav'); 
     $this->load->view('result',$data); 
     } 

    } 
} 
public function selected_flight() 
     { 
      $id = $this->uri->segment(3); 
      if($this->session->userdata('user') != true) 
      { 

       echo "<script> 
        window.alert('You need to login first!'); 
        location.href = '".site_url('User/search')."'; 
        </script>"; 

      } 
      else 
      { 
       $data['getFlight'] = $this->CrudModel->get_where('flight','flight_id',$id); 
       $this->load->view('partials/header'); 
       $this->load->view('partials/nav'); 
       $this->load->view('confirmation',$data); 
      } 


     } 

ビュー

<?php foreach($search_result as $result):?> 
      <div class="pure-u-1-1 resultborder"> 
       <img src="<?= base_url().'assets/images/aa.gif'?>" class="fleft"> 
       <table class="ml100"> 
        <tr> 
        <th>Flight number <?= $result->flight_number?></th> 
        <th>operated by: <?= $result->operated_by?></th> 
        <th><?= date('h:ia',strtotime($result->flight_time))?> - <?= date("l F j, Y",strtotime($result->flight_depart))?></th> 
        <th><?= $result->flight_from?></th> 
        <th>Seat Availability:<?= $result->seat_availability?></th> 

        <th><a href="<?= base_url(). 'User/selected_flight/'.$result->flight_id?>"><button class="btn btn-primary"><i class="fa fa-plane" aria-hidden="true"></i> Select Flight</button></a></th> 
        </tr> 

       </table> 
      </div> 
      <?php endforeach?> 
+0

私見あなたは、このようなデータを保存する場合 - あなたはのlocalStorage – sintakonte

+0

を使用する必要がありますあなたは何を意味するのlocalStorage – Angel

+0

https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage – sintakonte

答えて

0

をクリックした後、CodeIgniterでは、特定の期限付き“tempdata”、またはセッションデータをサポートしています時間。値の有効期限が切れた後、またはセッションが期限切れになるか削除されると、その値は自動的に削除されます。

tempdata変数は、flashdataと同様に、 '__ci_vars'キーの下に特定の方法でマークされた(通常、それに触れないでください)通常のセッション変数です。

セッションからtempdataの値を設定して取得できます。以下の解決策を見つけてください。

これは役に立ちます。

以下の解決策を見つけてください。

コントローラ

if($this->session->userdata('user') != true){ 
    // HERE you have to insert search params in TempData 
    $this->session->set_tempdata('search_param', $this->input->post(), 300); 
} 

public function search(){ 
    if($this->session->tempdata('search_param') !== NULL): 
     // assign values $flight_type, $pass_num etc 
     // set post data for form validation 
     $this->form_validation->set_data($this->session->tempdata('search_param')); 

     YOUR CODE 

     // Unset Tempdata after this 
     $this->session->unset_tempdata('search_param'); 
    endif; 
} 

であなたのsearch()機能は、次のようになります。

public function search() { 
    $data['countries'] = $this->CrudModel->get('countries'); 
    if ($this->session->tempdata('search_param') !== NULL): 
     $prevSearchData = $this->session->tempdata('search_param'); 
     $flight_type = $prevSearchData['flight_type']; 
     $pass_num = $prevSearchData['no_of_passengers']; 
     $this->form_validation->set_data($prevSearchData); 
    else: 
     $flight_type = $_POST['flight_type']; 
     $pass_num = $_POST['no_of_passengers']; 
    endif; 

    $testing = $this->session->set_userdata('pass_num', $pass_num); 
    $this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert" style="padding:0px;" >', '</div>'); 
    $this->form_validation->set_rules('flight_from', 'Select depature', 'required|trim'); 
    $this->form_validation->set_rules('flight_to', 'Select Destination', 'required|trim'); 
    if ($flight_type == 'round_trip') { 
     $this->form_validation->set_rules('depart', 'Date of flight', 'required|trim'); 
     $this->form_validation->set_rules('return', 'Date of return', 'required|trim'); 
     $this->form_validation->set_rules('no_of_passengers', 'Number of Passengers', 'required'); 
     if ($this->form_validation->run() == FALSE) { 
      $this->index(); 
     } else { 
      $search_result = array(
       $flight_from = $_POST['flight_from'], 
       $flight_to = $_POST['flight_to'], 
       $depart = $_POST['depart'], 
       $return = $_POST['return'], 
       $no_of_passengers = $_POST['no_of_passengers'] 
      ); 
      $data['search_result'] = $this->CrudModel->search_round_trip('flight', $flight_from, $flight_to, $depart, $return, $no_of_passengers); 
      $this->session->tempdata($data['search_result']); 
      $this->session->tempdata(); 
      $this->load->view('partials/header'); 
      $this->load->view('partials/nav'); 
      $this->load->view('result', $data); 
     } 
    } else { 
     $this->form_validation->set_rules('depart', 'Date of flight', 'required|trim'); 
     $this->form_validation->set_rules('no_of_passengers', 'Number of Passengers', 'required'); 
     if ($this->form_validation->run() == FALSE) { 
      $this->index(); 
     } else { 
      $search_result = array(
       $flight_from = $_POST['flight_from'], 
       $flight_to = $_POST['flight_to'], 
       $depart = $_POST['depart'], 
       $no_of_passengers = $_POST['no_of_passengers'] 
      ); 
      $data['search_result'] = $this->CrudModel->search_one_way('flight', $flight_from, $flight_to, $depart, $no_of_passengers); 

      $this->load->view('partials/header'); 
      $this->load->view('partials/nav'); 
      $this->load->view('result', $data); 
     } 
    } 
} 
+0

あなたは、利用可能なフライトのすべてをtempdataに入れますか? – Angel

+0

フライトのリストではなく、あなたの検索パラメータをtempdataに保存する必要があります。 –

+0

あなたが言ったように、私の検索方法では$ this-> session-> tempdata($ data ['search_result'])を挿入しました。 $ this-> session-> tempdata();. Btw、私は自分の投稿を編集しました。私は私の検索方法を入れた – Angel

関連する問題