1

Just want to pass checkbox array values to mysql database table after submitting the form (table columns: id, fanta, cola, sprite) . Each value should be inserted in seperate field (i.e. without using implode/explode functions). The best solution will be just passing "1" (if selected ) or "0" (if not selected). Please help me :)

Here is My Model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_example2 extends CI_Model {
  function __construct()
 {
  //Call the Model constructor
   parent::__construct();
 }
public function did_add() {
        $data = array(              
        'fanta' => $this->input->post('fanta'),
        'cola' => $this->input->post('cola'),
        'sprite' => $this->input->post('sprite'),         
                     );
        $query = $this->db->insert('table_example2', $data);
        if ($query) {
            return true;} 
        else {
        return false;}
    }
}

Here is My View:

             <div >
             <?php              
             $this->load->helper("form","file","html","url");
             echo $message;
             echo validation_errors();
             echo form_open("example2/add");
             echo form_label("Drink:<br>","type");
             ?>                   
<input type="checkbox" name="types[]" value="fanta" <?php echo set_checkbox('types[]', 'fanta', FALSE); ?>/>Fanta<br />
<input type="checkbox" name="types[]" value="cola" <?php echo set_checkbox('types[]', 'cola', FALSE); ?>/>Coca Cola<br />
<input type="checkbox" name="types[]" value="sprite" <?php echo set_checkbox('types[]', 'sprite', FALSE); ?>/>Sprite<br />
             echo form_submit("Submit", "Add");
             echo form_close();
             </div>

Here is My Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Example2 extends MX_Controller {
    public function add() {                 
            $this->load->library('form_validation');
            $this->load->model('model_example2');          
            $this->form_validation->set_rules('types[]', 'Drink','required');           
            if($this->form_validation->run()){
            $this->model_example2->did_add(); 
            $data["message"] = "Great job!";
            $this->load->view("view_add_success",$data);
            }
            else {
            $data["message"] = "";  
            $this->load->view("view_example2",$data);
            }           
    }
}
4
  • 1
    what is the column name to store type in db? do you have column name like "fanta" , "cola" etc..? if so, then you can name checkbox with fanta, cola etc.. Commented Feb 25, 2014 at 17:53
  • there are three column names: fanta, cola, sprite. I named checkbox "types" in order to validate checkbox fields (at least one should be selected (>=1)): $this->form_validation->set_rules('types[]', 'Drink','required'); Commented Feb 25, 2014 at 17:56
  • then you can name your checkbox with field names Commented Feb 25, 2014 at 18:00
  • but then I won't be able to validate checkbox group so that at least one should be selected ( >=1 ) Commented Feb 25, 2014 at 18:24

1 Answer 1

2

You can try like this

public function did_add() {

    $types = $this->input->post('types');
    $data = array(
        'fanta' => 0,
        'cola' => 0,
        'sprite' => 0,
    );
    foreach ($types as $type) {
        $data[$type] = 1;
    }

    $query = $this->db->insert('table_example2', $data);
    if ($query) {
        return true;
    }
    else {
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! Your code is the perfect solution of the above-mentioned problem. I really appreciate your help :)
Hi Minhaz, perhaps you could have a look on my question and help me to find the solution: stackoverflow.com/questions/22512209/…

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.