2016-04-05 153 views
0

查看輸入值返回

<div class="box-body"> 
    <div class="form-group"> 
     <label for="FNAME" class="col-sm-2 control-label col-sm-offset-2"> <span>*</span>First Name:</label> 
     <div class="col-sm-5"> 
      <input type="text" class="form-control" id="FNAME" name="FNAME"> 
     </div> 
    </div> 
    <div class="form-group has-error col-sm-offset-7"> 
     <label class="control-label" for="error"><?php echo form_error("FNAME"); ?></label> 
    </div> 
</div> 

控制器

public function create_id() { 

    $this->form_validation->set_rules('FNAME', 'First Name'   ,'trim|required|max_length[30]'); 
    $this->form_validation->set_rules('MNAME', 'Middle Name'   ,'trim|max_length[30]'); 
    $this->form_validation->set_rules('SURNAME', 'Last Name'    ,'trim|required|max_length[30]'); 

    if($this->form_validation->run($this) == FALSE) { 
     $this->add_view(); 
    } else { 
     if($query = $this->Employees_Model->insert()) { 
      $this->autoid(); 
      redirect('Employees/index'); 
     } else { 
      $this->add_view(); 
     } 
    } 
} 

我要的是if($this->form_validation->run($this) == FALSE){你看它會回重定向到視圖。我想要的全部是當它重新導向時,我輸入的數據將保持不變。這樣當驗證失敗時我不會再輸入它。

+0

這可以幫助你https://ellislab.com/笨/用戶指南/庫/ form_validation .html#repopulatingform – urfusion

回答

1

按照CodeIgniter的文件,你需要改變你的視圖文件一點點,在手段,在輸入element _value_ parameter打印這些輸入元素的值與<?= set_value("FNAME"); ?>。所以,你的

<input type="text" class="form-control" id="FNAME" name="FNAME"> 

將成爲

<input type="text" class="form-control" id="FNAME" name="FNAME" value="<?= set_value("FNAME"); ?>"> 

等。這樣你會告訴CodeIgniter在錯誤發生後重新填充表單。

+0

它在下拉菜單上工作嗎? –

+0

是的,但你需要更多的邏輯,因爲你需要檢查哪個**

0

您可以使用set_value函數。

視圖文件

<input type="text" class="form-control" id="FNAME" name="FNAME" value="<?php echo set_value('FNAME'); ?>"> 
0


獲取貼出值這樣

public function create_id() { 

    $this->form_validation->set_rules('FNAME',  'First Name'   ,'trim|required|max_length[30]'); 
    $this->form_validation->set_rules('MNAME',  'Middle Name'   ,'trim|max_length[30]'); 
    $this->form_validation->set_rules('SURNAME', 'Last Name'    ,'trim|required|max_length[30]'); 

    $data = array(
     'FNAME' => $this->input->post('FNAME'), 
     'MNAME' => $this->input->post('MNAME'), 
     'SURNAME' => $this->input->post('SURNAME') 
    ); 

    if($this->form_validation->run($this) == FALSE) { 
     $this->add_view($data); 
    } else { 
     if($query = $this->Employees_Model->insert()) { 
      $this->autoid(); 
      redirect('Employees/index'); 

     } else { 
      $this->add_view(); 
     } 
    } 
} 

在你的函數

function add_view($data){ 
    ............................. 
    ............................. 
    $this->data['values'] = $data; 
    ............................. 
    ............................. 

} 

然後

<div class="box-body"> 
     <div class="form-group"> 
     <label for="FNAME" class="col-sm-2 control-label col-sm-offset-2"><span>*</span>First Name:</label> 
      <div class="col-sm-5"> 
      <input type="text" class="form-control" id="FNAME" name="FNAME" value="<?= $values['FNAME'] ?>"> 
      </div> 
     </div> 
     <div class="form-group has-error col-sm-offset-7"> 
     <label class="control-label" for="error"><?php echo form_error("FNAME"); ?></label> 
     </div> 
    </div> 
+0

我得到了消息:'未定義的變量:數據'在'$ this-> add_view ($數據);' –