从复选框中检索数据

时间:2017-02-21 08:07:38

标签: php codeigniter checkbox codeigniter-3

我想从我的表中获取所有已检查的数据。我在表格中使用了复选框。当我点击更改状态时,它将更改我的角色状态。但我在检索复选框值时遇到问题。在下面的代码中,它无法更新我的数据。我检索的复选框值为NULL。请帮我解决问题。先谢谢

模型

function deaktifRole($id_role,$editBy)
{
    $data = array(
            'update' =>date('Y-m-d H:i:s'),
            'updateBy' =>$editBy,
            'flag' => '0'
        );

    $this->db->where('id_role',$id_role);
    $this->db->update('tbl_role',$data);
}

控制器

function deaktifRole()
{
    $session_data = $this->session->userdata('logged_in');


    $editBy = $session_data['username'];
    foreach ($this->input->post['pilih'] as $value) {
        $this->Role->deaktifRole($value->value,$editBy);
    }
    redirect('Home/Role');
    echo '<script>alert("Your form was successfully submitted!");</script>';
}

查看

<div class="x_panel">
              <div class="x_title">
                <h2>Manage Staff Role</small></h2>
                <?php echo form_open_multipart('Home/deaktifRole');?>  
                <div align="right">
                  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Add</button>
                  <button type="submit" class="btn btn-primary">Change Status</button>
                </div>
                <div class="clearfix"></div>
                </div>
              <div class="x_content">
                <table id="datatable-checkbox" class="table table-striped table-bordered bulk_action">
                  <thead>
                    <tr>
                      <th><input type="checkbox" id="check-all" class="flat"></th>
                      <th>No</th>
                      <th>Role</th>
                      <th>Status</th>
                    </tr>
                  </thead>
                  <tbody>
                  <?php $j=0; foreach ($RoleList as $rows)
                  {
                    $j++;
                  ?>
                    <tr>
                      <td><input type="checkbox" class="flat" name="pilih[]" value="<?php echo $rows['id_role']; ?>"></td>
                      <td><?php echo $j; ?></td>
                      <td><?php echo $rows['role']; ?></td>
                      <td>Aktif</td>
                    </tr>
                  <?php } ?> 
                  </tbody>
                </table>
              </div>
               <?php echo form_close(); ?>
            </div>
          </div>

    </div>

2 个答案:

答案 0 :(得分:0)

由于$this->input->post('pilih')只是一个关联数组,因此您可以直接访问它而不需要->value属性。尝试使用此代码:

foreach ($this->input->post('pilih') as $value) {
    $this->Role->deaktifRole( $value,$editBy );
}

答案 1 :(得分:0)

使用$this->input->post('pilih'); 获取复选框数据。