0

how to insert form values(dynamic checkbox,input) dynamically into database This my view file:

<form action="<?php base_url('controller/insert'); ?>"> 
    <table>
        <thead>
            <tr> 
                <th>Menu Id</th> 
                <th>Menu Name</th> 
                <th>Yes/No</th> 
            </tr> 
        </thead>
        <tbody>
            <?php foreach($result as $res) { ?>
                <tr> 
                    <td><input type="text" name="menu_id[]" value="<?= $res->menu_id ?>"></td> 
                    <td><input type="text" name="menu_name[]" value="<?= $res->menu_name ?>"></td> 
                    <td><input type="checkbox" name="yes[]" value="<?= $res->menu_id ?>"></td> 
                </tr> 
            <?php } ?>
        </tbody>
    </table>
</form>

this my controller function:

<?php
    $fields = array(
        'menu_id'   => $this->input->post('menu_id'),
        'menu_name' =>  $this->input->post('menu_name'),
        'yes'       =>$this->input->post('yes')
    );
    $this->db->insert('menu_table',$fields);
?>

when i print this array $fields it displays as:

Array ( [0] => 2 [1] => 3 ) Array ( [0] => Plant [1] => Line ) Array ( [0] => on [1] => on ).

this my form:the menuid and menu desc and checkbox displaying dynamically.i need to insert into table as same as the form below displays. enter image description here

3
  • Do you get an error? Are wrong values inserted? Is anything inserted? Commented Jul 23, 2018 at 7:42
  • 2
    you have to loop through your post value then insert the data since all form values are in arrays Commented Jul 23, 2018 at 7:43
  • no, i'm not getting any error,but i want to insert into db like the image mentioned above Commented Jul 23, 2018 at 7:46

1 Answer 1

3

You need to loop through the arrays. You can use the menu id as the key as it is sequential e.g. the first of menu_id corresponds with the first of menu_name:

$menu_id = $this->input->post('menu_id');
$menu_name = $this->input->post('menu_name');
$yes = $this->input->post('yes');

$yes = is_null($yes) ? array() : array_flip($yes);

if (is_null($menu_id) || is_null($menu_name)) {
    show_error('Parameters missing');
}

// should really be in a model
foreach ($menu_id as $key => $value) {
    $data['menu_id'] = $value;
    $data['menu_name'] = $menu_name[$key];
    $data['yes'] = isset($yes[$value]) ? 'true' : 'false';
    $this->db->insert('menu_table',$data);
}
Sign up to request clarification or add additional context in comments.

8 Comments

can you check something for me? your checkboxes are returning 'on' in your example. as you may know if a checkboxes isn't checked then it doesn't submit anything hence it might mess up the order and be incorrectly assigned in my answer. according to your value it should return the value of the menu_id? but it doesnt???
in any case if you can get the checkboxes to report the menu_id when checked you can simply modify my code as follows: $yes = array_flip($this->input->post('yes'));
but warning error occurs Message: array_flip() expects parameter 1 to be array, null given
my bad $yes = $this->input->post('yes'); $yes = is_null($yes) ? array() : array_flip($yes);
but if the checkbox isn't checked it stores false into table.(i replyed :can you check something for me?)
|

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.