Codeigniter - 多个复选框值返回NULL

时间:2017-04-03 07:17:22

标签: php codeigniter

我的View中有多个复选框,但在传递给Controller时会返回NULL值。

我搜索过(hereand here和其他网站)以查找我的问题的所有可能答案,但没有任何帮助。

以下是我的一些CI代码:

型号(My_Model)

public function get_query_array($query_statement)
{
    $query = $this->db->query($query_statement);
    return $query->result();
}

控制器(view_leave)

$data["leave_list"] = $this->My_Model->get_query_array("SELECT * FROM Leave_List WHERE Leave_Status='ACTIVE'");

$this->load->view('header_view');
$this->load->view('view_leave', $data);
$this->load->view('footer_view');

PHP / HTML(view_leave.php)

<form role="form" method="post" action="<?php echo base_url();?>index.php/home/update_leave" > 
<?php
    // assign all leave data into checkboxes and will be submitted to /updated_leave
    foreach($leave_list as $row)
    {
       echo "<input type='checkbox' name='leave_types[]' value='".$row->Leave_Code."'> ".$row->Leave_Description."<br/>";
    }
?>
<input type="submit" value="Submit" name="submit_form" id="submit_form">
</form>

控制器(update_leave)

$data = $this->input->post('leave_types');
var_dump($data);

// Here I got some errors when I checked on the boxes
// I use the code above to check if my check boxes are working
// Unfortunately, it returns a NULL value

为什么以及如何检查复选框值?

请帮忙。

谢谢!

3 个答案:

答案 0 :(得分:2)

我的样本工作

我的控制器

public function workout()
    {

        if($this->input->post())
        {
            echo "<pre>"; print_r($this->input->post()); exit();
        }

        $this->load->view('workout');
    }   

MyView的

<form name="customers" id="customers" method="post" action="<?php echo base_url(); ?>welcome/workout"  enctype="multipart/form-data"   >


<input type='checkbox' name='leave_types[]' value='1'>1<br/>

<input type='checkbox' name='leave_types[]' value='2'>2<br/>

<input type='checkbox' name='leave_types[]' value='3'>3<br/>

<input type='checkbox' name='leave_types[]' value='4'>4<br/>

<input type='checkbox' name='leave_types[]' value='5'>5<br/>


<input type='submit' name='submit' value='Submit'>Submit<br/>


    </form>

当我运行时,我的输出是

Array
(
    [leave_types] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [submit] => Submit
)

试试这希望它有所帮助。 :)

答案 1 :(得分:0)

型号(My_Model)

public function get_query_array()
{
    $this->db->where('Leave_Status', 'ACTIVE');
    $query = $this->db->get('Leave_List');
    return $query->result();
}

控制器(View_leave)

$data["leave_list"] = $this->My_Model->get_query_array();

$this->load->view('header_view');
$this->load->view('view_leave', $data);
$this->load->view('footer_view');

PHP / HTML(view_leave.php)

<?php
// assign all leave data into checkboxes and will be submitted to /updated_leave
foreach($leave_list as $row)
{
   echo "<input type='checkbox' name='leave_types[]' value='".$row->Leave_Code."' /> ".$row->Leave_Description."<br/>";
}
?>

控制器(update_leave)

$data = $this->input->post('leave_types[]');
var_dump($data);

答案 2 :(得分:0)

像这样更改视图

<?php $attributes = array('name'=>'leave_form','id'=>'leave_form');
        echo form_open('home/update_leave',$attributes);?>

<?php
if(isset($leave_list)){
foreach($leave_list as $row): ?>

<input type='checkbox' name='leave_types[]' value='<?php echo $row->Leave_Code; ?>' /> <?php echo $row->Leave_Description; ?>
<?php endforeach; } ?>
 ?>