0

我试图在这段代码中为我的复选框(1 = 选中或 0 = 未选中)获取值(这是我在 Code Igniter 中的视图):

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

我的问题是:

  1. 有人可以向我解释为什么 $visible 的结果总是 NULL 吗?
  2. 在这种情况下的功能是什么value="1"?我正在搜索这个案例,但人们只是说我们总是需要添加价值。
  3. 如何获得正确的值?(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');
    }
}

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

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

1 回答 1

0

请改用输入库

特别是 post 功能

$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');
    }
}
于 2012-09-09T11:37:43.843 回答