一個繼續的問題的情況是:之後我添加了一個隱藏字段,現在我可以更新,但我不能保存,因爲每次我試圖挽救,這個錯誤消息不斷顯示出來:The User ID field is required.
這是控制器,附加功能是保存和更新更新:
function add(){
//set common properties
$data['title'] = 'Tambah User baru';
$data['action'] = site_url('user/add');
$data['link_back'] = anchor('user/index/', 'Back to User list', array('class'=>'back'));
//bedakan add/update
$data['validate'] = 'add';
$this->_set_rules();
//run validation
if($this->form_validation->run() == false){
$data['message'] = '';
$data['title'] = 'Add new User';
//$data['message'] = '';
$data['user']['ID_user'] = '';
$data['user']['pass'] = '';
$data['user']['nama'] = '';
$data['user']['email'] = '';
$data['user']['active'] = '';
$data['link_back'] = anchor('user/index/', 'Lihat daftar User', array('class'=>'back'));
$this->load->view('user_form_v', $data);
}
else{
//save data
$user = array('ID_user'=>$this->input->post('ID_user'),
'pass'=>sha1($this->input->post('pass')),
'nama'=>$this->input->post('nama'),
'email'=>$this->input->post('email'),
'active'=>$this->input->post('active'),
'regis_date'=>date('Y-m-d H:i:s'));
$ID_user = $this->user_m->save($user);
//set form input nama = "id"
$this->validation->ID_user = $ID_user;
redirect('user/index/add_success');
}
}
function update($ID_user){
//set common properties
$data['title'] = 'Update user';
$this->load->library('form_validation');
//set validation properties
$this->_set_rules();
$data['action'] = ('user/update/'.$ID_user);
//bedakan add/update
$data['validate'] = 'update';
//run validation
if ($this->form_validation->run() == false){
$data['message'] = '';
$data['user'] = $this->user_m->get_by_id($ID_user)->row_array();
//set common properties
$data['title'] = 'Update User';
$data['message'] = '';
}
else{
//save data
$ID_user = $this->input->post('ID_user');
$user = array(
'pass'=>$this->input->post('pass'),
'nama'=>$this->input->post('nama'),
'email'=>$this->input->post('email'),
'active'=>$this->input->post('active'),
'regis_date'=>date('Y-m-d H:i:s'));
$this->user_m->update($ID_user, $user);
$data['user'] = $this->user_m->get_by_id($ID_user)->row_array();
//set user message
$data['message'] = 'Update User Success!';
}
$data['link_back'] = anchor('user/index/', 'Lihat daftar user', array('class'=>'back'));
//load view
$this->load->view('user_form_v', $data);
}
這是我的看法:
<input type="text" name="ID_user" class="text"
<?php if($validate!='add'){echo "disabled";} ?>
value="<?php echo (isset($user['ID_user']))?$user['ID_user']:""?>"/>
<input type="hidden" name="ID_user" value="<?php echo (isset($user['ID_user']))?$user['ID_user']:""?>"/>
注:如果隱藏輸入文本和輸入的名稱相同,則更新工作,但保存不工作,但如果名稱不同,則更新沒有工作,但保存工作@_ @
非常感謝你,你救了我的一天爵士:d 順便說一句,你能解釋一下在哪裏插入一個變量到我的隱藏元素?我真的不明白這個代碼,特別是隱藏的輸入一個 –
@BlazeTama你是什麼意思?您已經爲代碼中的隱藏輸入輸出了必要的變量。 –
好吧,我現在緩慢地得到它..謝謝:D –