2016-04-05 157 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 ($数据);' –