0

I am trying to get multiple values from a foreach loop generated check boxes. Please check my codes below and give me some suggestion. Thanks.

View file

<?php foreach ($size_list as $size) { ?>
<label><input name="size_id" type="checkbox" value="<?php echo $size->size_id;?>" />
<?php echo $v_size_list->size;?></label>
<?php } ?>

When i submit this form after selecting multiple checkboxes, i got only the last check box value. But i want all selected check box values. Please give me some suggestion. Thanks

Sumon
  • 301
  • 2
  • 4
  • 11

3 Answers3

1

use array in name like

name="size_id[]"

and you will get all selected checkbox value in array.

pratik
  • 49
  • 1
  • 7
  • Thanks. It works. i got the value and then i use implode function in controller to separate the values. :) – Sumon May 30 '16 at 12:33
1

use change your checkbox name into array and it contation all the checkbox value in array structure which you checked in form

name="size_id[ ]"

<?php foreach ($size_list as $size) { ?>

<label><input name="size_id[]" type="checkbox" value="<?php echo $size->size_id;?>" />
<?php echo $v_size_list->size;?></label>

and get the value by

print_r($_POST['size_id']);

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

My suggestion is to check this answer: Get $_POST from multiple checkboxes

So your checkboxes name have to be size_id[], and in the controller, where you check the value of the checkbox, you have to loop this with a foreach.

public function posted()
{
    $checkboxes = $this->input->post("size_id");
    foreach($checkboxes as $checkbox)
    {
        // in this loop you can check the value of the selected checkbox
    }
}
Community
  • 1
  • 1
skyyler
  • 165
  • 10