如何在 codeigniter 中重用表单进行更新和添加
How to reuse form in codeigniter for update and add
你好,我想在 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 class 的 set_value() 方法,因为如果更新,它会与填充表单发生冲突。
在您的控制器更新函数中添加如下条件:
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);?>
你好,我想在 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 class 的 set_value() 方法,因为如果更新,它会与填充表单发生冲突。
在您的控制器更新函数中添加如下条件:
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);?>