isset复选框返回null代码点火器

时间:2012-09-09 11:32:19

标签: php codeigniter

我试图在此代码中获取我的复选框(1 =已选中或0 =取消选中)的值(这是我的代码点火器中的视图):

<input type="checkbox" name="Visible" class="text" value="1"
                <?php $visible = isset($_POST['Visible'])? '1' : '0';?>/>

我的问题是:

  1. 有人可以向我解释为什么$ visible的结果总是为空吗?
  2. 在这种情况下value="1"的功能是什么?我正在搜索此案例,但人们只是说我们总是需要添加值。
  3. 如何获得正确的值?(1 =选中或0 =未选中)
  4. 谢谢你,我知道这是一个非常简单的问题,但是自昨天以来它一直给我一些错误@ _ @

    这是代码的重要部分: 添加功能用于保存(在控制器中):

    function add(){
    
        $data['title'] = 'Tambah Category baru';
        $data['action'] = site_url('category/add');
        $data['link_back'] = anchor('category/index/', 'Back to Category 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 Category';
            //$data['message'] = '';
            $data['category']['ID_category'] = '';
            $data['category']['ID_user'] = '';
            $data['category']['Nama_category'] = '';
            $data['category']['Description'] = '';
            $data['category']['Visible'] = '';
            $data['link_back'] = anchor('category/index/', 'Lihat daftar Category', array('class'=>'back'));
    
            $this->load->view('category_form_v', $data);
        }
    
        else{
            //save data
            $category = array('ID_category'=>$this->input->post('ID_category'),
            'ID_user'=>'TESTING USER',
            'Nama_category'=>$this->input->post('Nama_category'),
            'Description'=>$this->input->post('Description'),
            'Visible'=>($$this->input->post('Visible') !== false ? '1' : '0'),
            'tgl_buat '=>date('Y-m-d H:i:s'));
    
            $ID_category = $this->category_m->save($category);
    
            $this->validation->ID_category = $ID_category;
    
            redirect('category/index/add_success');
        }
    }
    

    我的HTML代码(在视图中):

    <td valign="top">Visible<span style="color:red;">*</span></td>
                    <td><input type="checkbox" name="Visible" class="text"/>
    

1 个答案:

答案 0 :(得分:0)

改为使用input library

特别是帖子功能

$visible = ($this->input->post('Visible') !== false ? 1 : 0);

问题更新后更新

function add(){

    $data['title'] = 'Tambah Category baru';
    $data['action'] = site_url('category/add');
    $data['link_back'] = anchor('category/index/', 'Back to Category 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 Category';
        //$data['message'] = '';
        $data['category']['ID_category'] = '';
        $data['category']['ID_user'] = '';
        $data['category']['Nama_category'] = '';
        $data['category']['Description'] = '';
        $data['category']['Visible'] = '';
        $data['link_back'] = anchor('category/index/', 'Lihat daftar Category', array('class'=>'back'));

        $this->load->view('category_form_v', $data);
    }

    else{
        //save data
        $category = array('ID_category'=>$this->input->post('ID_category'),
        'ID_user'=>'TESTING USER',
        'Nama_category'=>$this->input->post('Nama_category'),
        'Description'=>$this->input->post('Description'),
        'Visible'=>($this->input->post('Visible') !== false ? '1' : '0'),
        'tgl_buat '=>date('Y-m-d H:i:s'));

        $ID_category = $this->category_m->save($category);

        $this->validation->ID_category = $ID_category;

        redirect('category/index/add_success');
    }
}