Welcome Guest, Not a member yet? Register   Sign In
Codeigniter passing checkbox array values to mysql database
#1

[eluser]Unknown[/eluser]
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 Smile

Here is My Model:

Code:
<?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:

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

Code:
&lt;?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);
            }          
    }
}
#2

[eluser]comyou[/eluser]
I haven't tested it but I'm sure you should be checking if it's checked or not.

Try something like:
Code:
if(isset($this->input->post('fanta'))) {
                $data['fanta'] = '1';
} else {
                $data['fanta'] = '0';
};

That should check if the checkbox is checked and return '1' is so, that way any value in your Database other than '1' is a false.

Apologies if it doesn't work.
#3

[eluser]CroNiX[/eluser]
[quote author="comyou" date="1393375720"]I haven't tested it but I'm sure you should be checking if it's checked or not.

Try something like:
Code:
if(isset($this->input->post('fanta'))) {
                $data['fanta'] = '1';
} else {
                $data['fanta'] = '0';
};

That should check if the checkbox is checked and return '1' is so, that way any value in your Database other than '1' is a false.

Apologies if it doesn't work.[/quote]
No, that actually won't work. Since input::post() will return boolean FALSE if the field was not in $_POST, or returns the value if it does, it will always be "set" so isset() test will always be true.
#4

[eluser]CroNiX[/eluser]
I don't know why you are using an array for this case.

If your checkboxes were like:
Code:
&lt;input type="checkbox" name="fanta" value="1" &lt;?php echo set_checkbox('fanta', '1', FALSE); ?&gt;/&gt;Fanta

Then in your controller you'd just do:
Code:
$data = array(              
        'fanta' => ($this->input->post('fanta') === FALSE) ? 0 : 1,
        //same for other checkboxes...
);

Checkboxes don't get transmitted via $_POST unless they are actually checked. So, if it isn't checked, then input::post('fanta') will be boolean FALSE. If it's boolean false, use a 0, else use 1 as it was checked.
#5

[eluser]Unknown[/eluser]
Maybe you should check that through below way

Code:
$types = $this->input->post("type"); // It will be array

if( ! is_array($types)) throw new Exception("Type is required");




Theme © iAndrew 2016 - Forum software by © MyBB