codeigniter 从复选框检索数据

yyyllmsg  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(136)

我想从我的表中获取所有选中的数据。我在我的表中使用复选框。当我单击“更改状态”时,它将更改我的角色状态。
但是我在检索复选框值时遇到了问题。在下面的代码中,它无法更新我的数据。我检索到的复选框值为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>
vxf3dgd4

vxf3dgd41#

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

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

p4rjhz4m2#

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

相关问题