2017-06-09 13 views
2

私は航空会社の予約をしていますが、2つのラジオボタンがあります。フィールドを非表示にしたときの妥当性検査を防止Codeigniter

1)

2つの方法)往復

私がやった事は、私は往復を選択すると、すべてのフィールドが乗客の存在(出発、リターンと数あるある)が、片方向ラジオボタンを選択すると、リターンフィールドは非表示になります。

私のコントローラでは、すべてのフィールドが必要であることを確認しています。問題は、ワンウェイで検索しようとするたびに(返されたフィールドが非表示になっている)返されたフィールドが必要です。エラー

質問:私が選択したときに、道のラジオボタン?

ビュー

<div class="pure-u-1-1 searchcontainer center"> 
      <div class="pure-u-1-1 findcheaptxt"> 
       <span>Find Cheap Flights</span> 
      </div> 
      <div class="pure-u-1-1 radiobtn"> 
       <form action=""> 
        <input type="radio" name="flight_type" value="one_way" class="onew" style="" >One Way 
        <input type="radio" name="flight_type" class="roundw" style="" checked>Round Trip 
       </form> 
      </div> 

      <form method="post" enctype="multipart/form-data" action="<?= base_url() .'User/search'?>"> 
      <?= validation_errors(); ?> 
      <div class="pure-u-1-1 fromto"> 
       <div class="pure-u-1-1"> 
        <label for="from" class="margin2px">From</label> 

        <select name="flight_from"> 
        <option value="">-- Please select depature --</option> 
        <?php foreach($countries as $country):?> 
         <option value ="<?= $country->country_name?>" ><?= $country->country_name?></option> 
        <?php endforeach?> 

        </select> 

       </div> 
       <div class="pure-u-1-1"> 
        <label for="to" class="tomargin">To</label> 
        <!-- <input type="text" class="fromto"><br> --> 
        <select class="fromto" name="flight_to"> 
        <option value="">-- Please select destination --</option> 
        <?php foreach($countries as $country):?> 
         <option value ="<?= $country->country_name?>" ><?= $country->country_name?></option> 
        <?php endforeach?> 
        </select> 
       </div> 
       <div class="pure-u-1-1 dr" name ="depart"> 
        <label for="depart" class="drr">Depart</label> 
        <input type="date" id="depart" name="depart" class="departreturn"> 

       </div> 
       <div class="pure-u-1-1 dr" id="try"> 
        <label for="return" class="drr">Return</label> 
        <input type="date" id="return" name="return" class="departreturn"><br> 

        </div> 
      </div> 

      <div class="pure-u-1-1 personfield"> 
       <!-- <div class="pure-u-1-5 margin"> 
        Adult<br> 
        <input type="text" name="" class="person"> 
       </div> 
       <div class="pure-u-1-5 margin"> 
        Seniors<br> 
        <input type="text" name="" class="person"> 
       </div> 
       <div class="pure-u-1-5 margin"> 
        Children<br> 
        <input type="text" name="" class="person"> 
       </div> 
       <div class="pure-u-1-5"> 
        Class<br> 
        <input type="text" name="" class="person"> 
       </div> --> 
        <div class="pure-u-1-5 margin"> 
        Number of Passengers<br> 
        <input type="text" name="no_of_passengers" class="person"> 
       </div> 

      </div> 
       <div class="pure-u-1-1 center"> 
         <button class="submitbtn">Search Now</button> 
       </form> 
      </div> 
     </div> 
     </div> 

リターンフィールド

<script type="text/javascript"> 
$(document).on('change', 'input:radio[name=flight_type]', function(){ 
    $('div[id^="try"]').toggle(); // hide all DIVs begining with "my_radio_" 
    $('#' + $(this).attr('id') + '_text').show(); // show the current one 
}); 
</script> 
+0

あなたは、往復のため –

答えて

2

あなたは往復に値を追加する必要がまず第一に

// Global validation for this method 
if ($_POST['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'); 
    } 
    else if($_POST['flight_type'] == 'one_way'){ 
     // add validation for one way trip 
    } 
+0

こんにちは、なぜ私は値= "round_trip"を入れる必要がありますか?私は他の人が必要なのはname = "*"です。 – Angel

+1

往復を確認してフォームを送信すると、$ _POST ['flight_type']を得ることができないため、追加する必要があるからです。この簡単なページhttps://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_completeをご覧ください。 –

+0

こんにちは、 "メッセージ:未定義インデックス:flight_type"コントローラ/ユーザーライン28 ..私の間違いは何ですか?私は私の投稿を更新しました – Angel

2

を隠し

public function search() 
{ 
    $data['countries'] = $this->CrudModel->get('countries'); 
    $this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>'); 
    $this->form_validation->set_rules('flight_from', 'Select depature', 'required|trim'); 
    $this->form_validation->set_rules('flight_to', 'Select Destination', 'required|trim'); 

    if ($_POST['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('flight',$flight_from,$flight_to,$depart,$return,$no_of_passengers); 
      $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('flight',$flight_from,$flight_to,$depart,$no_of_passengers); 
     $this->load->view('partials/header'); 
     $this->load->view('partials/nav'); 
     $this->load->view('result',$data); 
     } 

    } 
} 

私のAJAX/JS

コントローラあなたはポストリクエストで flight_typeパラメータを渡さなければなりません。そうすれば、あなたのコードは以下のようになります。

$this->form_validation->set_rules('flight_from', 'Select depature', 'required|trim'); 
$this->form_validation->set_rules('depart', 'Date of flight', 'required|trim'); 

if($this->input->post('flight_type')== 'roundw'): 
    $this->form_validation->set_rules('flight_to', 'Select Destination', 'required|trim'); 
    $this->form_validation->set_rules('return', 'Date of return', 'required|trim'); 
endif: 

あなたにはうまくいかない場合は教えてください。その後

<input type="radio" name="flight_type" value="round_trip" class="roundw" style="" checked>Round Trip 

以下のような検証条件を実行します:

+0

こんにちはSIRを付加価値をやりなさいません私はすでにそれを試みましたが、値を表示しませんでした。 – Angel

+0

あなたのモデル検索機能を共有してください。モデルパーツに何らかのエラーがあるようです。 –

+0

@ahmed ginaniが言ったように、あなたは$ _POST ['flight_type']を得ることができないので、私の見解に価値を追加する必要があります – Angel

関連する問題