2017-06-09 16 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> 

控制器

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隱藏的返回字段

<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

首先你增加價值往返:

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

然後做驗證條件如下圖所示:

// 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」?我的意思是其他人只需要名稱=「*」.. – Angel

+1

因爲如果你檢查往返和提交表單,你不能得到$ _POST ['flight_type'],這就是爲什麼你必須添加。看看這個簡單的頁面https://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_complete。 –

+0

嗨,先生,我有一個錯誤「消息:未定義的索引:flight_type」控制器/用戶行28 ..我的錯誤是什麼?我更新了我的帖子 – Angel

2

時您必須在郵寄請求中傳遞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: 

讓我知道它是否不適合你。

+0

先生你好增加價值,我已經嘗試過,但它沒有顯示數值。 – Angel

+0

您可以請分享您的模型搜索功能。因爲你似乎在模型部分有一些錯誤。 –

+0

@ahmed ginani說,你不能得到$ _POST ['flight_type']這就是爲什麼我需要在我的視圖中添加價值 – Angel