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

1
  • use array like this name="size_id[]" Commented May 30, 2016 at 12:17

3 Answers 3

1

use array in name like

name="size_id[]"

and you will get all selected checkbox value in array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It works. i got the value and then i use implode function in controller to separate the values. :)
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']);

Comments

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
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.