How to get checkbox value in controller codeigniter?, |
05-29-2019, 02:40 PM
(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[]'), );
I don't see a question in your post, or any indication of what might be going wrong??
I think
'category' => $this->input->post('category[]'), should be 'category' => $this->input->post('category'),
Simpler is always better
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 )
(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. |
Welcome Guest, Not a member yet? Register Sign In |