2016-03-01 35 views
0

您好,我希望重複使用codeigniter中的表單,但問題在於更新表單已填充頁面已加載。這是一個示例表單。這裏如何在codeigniter中重新使用表單進行更新並添加

<?php 
    $attributes = array('class' => 'form-horizontal', 'id' => 'addPersonnel'); 
    echo form_open($form_submission, $attributes); 
    ?> 
     <input type="text" name="name" value="<?php echo set_value('name')?"> > 
     <input type="text" name="rank" value="<?php echo set_value('rank')?"> > 
     <button type="submit" class="btn btn-primary">Save</button>             
    <?php echo form_close()?> 

我的問題就出在form_validation類的SET_VALUE()方法,因爲它會與填充如果更新形式的衝突。

回答

1

在您的控制器更新功能添加像一個條件:

if ($this->input->server('REQUEST_METHOD') === 'POST') { 
    $this->_get_userinfo_form_fields(); 
} else { 
    $this->_get_userinfo_form_fields($user_data); //$user_data from database 
} 

對於附加功能,您可以直接調用 - $this->_get_userinfo_form_fields();

然後聲明如下功能:

protected function _get_userinfo_form_fields($user_data = array()) { 
    $this->data['rank'] = array(
     'placeholder' => 'Rank', 
     'name'  => 'rank', 
     'id'   => 'rank', 
     'class'  => '', 
     'value'  => isset($user_data['rank']) ? $user_data['rank'] :set_value('rank',$this->input->post('rank',TRUE)), 
     'style'  => '', 
    ); 

    //Add rest of the fields here like the above one 
} 

鑑於文件添加代碼如下:

<?php echo form_input($rank);?> 
相關問題