droxy80248 2012-09-08 10:10
浏览 31
已采纳

隐藏类型代码点火器的表单验证错误

This is a continue question from here

The case is : after i add a hidden field, now i could Update but i CANT save because everytime i try to save, this error message keep showing up : The User ID field is required.

This is the controller, function add is for Save and update for 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);
}

And this is my view :

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

note : if the name for input text and input hidden is SAME then the update is working but the save is not working, but if the name is different then the update is not working but the save is working @_@

  • 写回答

2条回答 默认 最新

  • douyeyan0650 2012-09-08 10:17
    关注

    Just put the hidden input element above the disabled element.

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

    The issue appears because the value is overwritten by the hidden input element (which is empty in your "save" page). By moving placing the hidden one before the may or may not be disabled one, both of the "save" and "update" page will work correctly. In the update page, it'll work because if the input is disabled, it will not get sent to the server (i.e. it will not overwrite the hidden one).

    However, in my opinion, using a hidden input is not very secure. Users can easily change the value by editing the HTML using tools like Firebug or Chrome developer tools. You might want to look into session as a solution instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)