Welcome Guest, Not a member yet? Register   Sign In
How to get checkbox value in controller codeigniter?,
#1

(This post was last modified: 05-29-2019, 03:39 PM by Senior Code. Edit Reason: The value i want to get is from category. )

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[]'),

              );
Reply
#2

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

(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.
Reply
#4

I think

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

should be

'category' => $this->input->post('category'),
Simpler is always better
Reply
#5

(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.
Reply
#6

(This post was last modified: 05-30-2019, 03:56 AM by InsiteFX.)

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(This post was last modified: 05-30-2019, 05:53 AM by dave friend.)

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB