CodeIgniter Forums
How to get checkbox value in controller codeigniter?, - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to get checkbox value in controller codeigniter?, (/showthread.php?tid=73745)



How to get checkbox value in controller codeigniter?, - Senior Code - 05-29-2019

This is the view

</div>
          <div>
            <input type="checkbox" name="category[]" value="1"><label>Abarrotes</label>
            <input type="checkbox" name="category[]" value="2"><label>Frutas y verduras</label>
            <input type="checkbox" name="category[]" value="3"><label>Cárnicos</label>
            <input type="checkbox" name="category[]" value="4"><label>Barismo</label>
            <input type="checkbox" name="category[]" value="5"><label>Limpieza</label>
         </div>
</div>

This is the controller


         $this->form_validation->set_rules('category[]', 'Category', 'required');

        if ($this->form_validation->run() == TRUE) {
               $data = array(

                         'category' => $this->input->post('category[]'),

              );


RE: get checkbox value in controller codeigniter - ciadmin - 05-29-2019

I don't see a question in your post, or any indication of what might be going wrong??


RE: get checkbox value in controller codeigniter - Senior Code - 05-29-2019

(05-29-2019, 03:25 PM)ciadmin Wrote: I don't see a question in your post, or any indication of what might be going wrong??

Sorry and thank you.


RE: How to get checkbox value in controller codeigniter?, - donpwinston - 05-29-2019

I think

'category' => $this->input->post('category[]'),

should be

'category' => $this->input->post('category'),


RE: How to get checkbox value in controller codeigniter?, - Senior Code - 05-29-2019

(05-29-2019, 04:48 PM)donpwinston Wrote: I think

'category' => $this->input->post('category[]'),

should be

'category' => $this->input->post('category'),

I did it that way but it did not work, thanks.


RE: How to get checkbox value in controller codeigniter?, - InsiteFX - 05-30-2019

Do a var_dump to see what is there.

PHP Code:
var_dump($data['category']); 

Also note that if a checkbox is not checked it has no value at all.


RE: How to get checkbox value in controller codeigniter?, - dave friend - 05-30-2019

(05-30-2019, 03:55 AM)InsiteFX Wrote: Also note that if a checkbox is not checked it has no value at all.

Unchecked boxes have no value and are not sent to the server. Given 5 checkboxes, if only two are checked then the return from $this->input->post('category') will be an array with two items. If no boxes are checked the return from $this->input->post('category') would be null.