CodeIgniter Forums

Full Version: How to get checkbox value in controller codeigniter?,
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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[]'),

              );
I don't see a question in your post, or any indication of what might be going wrong??
(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.
I think

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

should be

'category' => $this->input->post('category'),
(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.
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.
(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.