0

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);
    }
1
  • Turns out my code works fine; there was a syntax error I caught. Commented Jan 25, 2012 at 14:12

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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>

Comments

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.