0

这是从这里开始的一个继续问题

情况是:添加隐藏字段后,现在我可以更新但我无法保存,因为每次我尝试保存时,都会出现此错误消息:The User ID field is required.

这是控制器,函数 add 用于 Save 和 update 用于 Update :

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']:""?>"/>

注意:如果输入文本和输入隐藏的名称相同,则更新有效但保存无效,但如果名称不同,则更新无效但保存有效@_@

4

2 回答 2

1

只需将隐藏的输入元素放在禁用的元素上方即可。

<input type="hidden" name="ID_user" value="<?php echo (isset($user['ID_user']))?$user['ID_user']:""?>"/>
<input type="text" name="ID_user" class="text"
                <?php if($validate!='add'){echo "disabled";} ?>
                value="<?php echo (isset($user['ID_user']))?$user['ID_user']:""?>"/>

出现问题是因为该值被隐藏的输入元素(在“保存”页面中为空)覆盖。通过将隐藏的页面移动到可能禁用或可能不禁用的页面之前,“保存”和“更新”页面都将正常工作。在更新页面,它会起作用,因为如果输入被禁用,它不会被发送到服务器(即它不会覆盖隐藏的)。

但是,在我看来,使用隐藏输入并不是很安全。用户可以通过使用 Firebug 或 Chrome 开发人员工具等工具编辑 HTML 轻松更改值。您可能希望将会话视为解决方案。

于 2012-09-08T10:17:56.567 回答
1

你应该为输入文本提供不同的名称,输入隐藏。你给了两个“ID_user”。试试这样

 <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_hidden" value="<?php echo            (isset($user['ID_user']))?$user['ID_user']:""?>"/>
于 2012-09-08T10:19:04.823 回答