chowwy
chowwy

Reputation: 1126

Passing checkbox values to mysql database using Codeigniter

I'm using CodeIgniter and mySQL to build a checkbox form. The form contains 4 options; each option has only one checkbox; users can select any combination of the options. I want to do the following:

1 - For each checkbox, use a value of 1 (if unchecked) or 2 (if checked) and pass those values through to the database (each checkbox has its own field). Right now, whether checked or unchecked, the checkboxes are sending a value of 0 through to the database.

2 - Once users update their checkboxes, I'd like to update the database to reflect the new values. Right now, a new row is added for each update to the checkboxes.

What I've got so far is a form that submits the checkbox values to the database, a controller, and a model):

Form

<?php echo form_open('addFoo'); ?>
<input type="checkbox" name="foo1" value="" />
<input type="checkbox" name="foo2" value="" />
<input type="checkbox" name="foo3" value="" />
<input type="checkbox" name="foo4" value="" />
<?php echo form_submit('submit', 'Save Changes'); ?>
<?php echo form_close(); ?>

Controller

function addFoo()
{
    if ($this->input->post('submit')) {
        $id = $this->input->post('id');            
                $foo1 = $this->input->post('foo1');
                $foo2 = $this->input->post('foo2');
                $foo3  = $this->input->post('foo3');
                $foo4  = $this->input->post ('foo4');

    $this->load->model('foo_model');
    $this->foo_model->addFoo($id, $foo1, $foo2, $foo3, $foo4);
    }
}

Model

function addFoo($id, $foo1, $foo2, $foo3, $foo4) {
        $data = array(
            'id' => $id,
            'foo1' => $foo1,
            'foo2' => $foo2,
            'foo3' => $foo3,
            'foo4' => $foo4
        );

        $this->db->insert('foo_table', $data);
    }

Upvotes: 0

Views: 12385

Answers (3)

srbhbarot
srbhbarot

Reputation: 1317

At your Controller :

if you want to insert new entry for all selected checkbox:

foreach($this->input->post('foo') as $r)
{
 $data['fieldname']=$r;

 $this->model_name->insert($data);
}

if you want to insert all selected checkbox values in different fields within single entry then,

foreach($this->input->post('foo') as $key=>$val)
    {
     $data['field'.$key]=$val;

    }
$this->model_name->insert($data);

Upvotes: 3

Dau
Dau

Reputation: 8858

use the different values for each checkbox and get the value of checkbox array and use this in your controller print_r($this->input->post('foo')); it will print the values that are selected by user

use like this

<?php
    if (isset($_POST['submit'])){
        print_r($_POST['foo']);
    }
?>
<form action="" method="POST">
<input type="checkbox" name="foo[]" value="1">1<br>
<input type="checkbox" name="foo[]" value="2">2<br>
<input type="checkbox" name="foo[]" value="3">3<br>
<input type="checkbox" name="foo[]" value="4">4<br>
<input type="submit" value="submit" name="submit">
</form>

Upvotes: 1

hmind
hmind

Reputation: 880

Well in reference to setting the values, a checkbox doesn't send anything if unchecked. To achieve what you want, you have to do this:

<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="2" />

This will send a value regardless of whether the checkbox is checked or not.

Upvotes: 1

Related Questions