0

I have multiple check boxes from my View but it returns NULL value when passed to Controller.

I've searched (here, and here, and from other sites) for all possible answers to my problem but nothing helped.

Here are some of my CI codes:

Model (My_Model)

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

Controller (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>

Controller (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

Why and how can I check for the checkbox values?

Please help.

Thanks!

Community
  • 1
  • 1
Jorz
  • 358
  • 5
  • 22
  • can you inspect the dropdown box and see whether the select box actually contains the values. and if everything looks good then post your form code too that make post request to the controller. – BetaDev Apr 03 '17 at 07:22

3 Answers3

2

my Sample work out

my controller

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>

and when i run this my output is

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

    [submit] => Submit
)

Try this Hope it helps. :)

Dinesh Kumar
  • 649
  • 3
  • 8
  • This works fine. I can use this as my guide but I'll try to merge this first into my code so I can decide if I'll accept this one. Anyway, thanks a lot for this! =) – Jorz Apr 03 '17 at 07:56
0

Model (My_Model)

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

Controller (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/>";
}
?>

Controller (update_leave)

$data = $this->input->post('leave_types[]');
var_dump($data);
Gaurav
  • 721
  • 5
  • 14
0

Change the view like this

<?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; } ?>
 ?>
Gollapalli
  • 20
  • 3
  • Why should the OP "change the view"? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Apr 03 '17 at 13:03
  • @JayBlanchard I a have changed the form open from html tag to codeigneter form tag this will be usefull for csrf token for security purpose and second thing what i have add isset for checking the leave list, this is very usefull for checking the whether variable $leave_list is set or not, if it is not set it gives the error if we have not used isset , third thing what i have changed is checkbox as html check box , user can easily understand the html check box – Gollapalli Apr 03 '17 at 13:15
  • Put that explanation in your answer. – Jay Blanchard Apr 03 '17 at 13:17