Welcome Guest, Not a member yet? Register   Sign In
How to save multiple checkbox?
#1

[eluser]Wondering Coder[/eluser]
Running out of time here so here goes coz I have a defense the day after tomorrow. So here goes: I'm trying to save a multiple checkbox but somehow cant get it right.

Scenario:
1. Let's say I have 2 rows and I check at least 1 checkbox per row then save my setting (saved and ok so far)
2. Now If I only chose to check the 2nd row (leaving the 1st row all uncheck), it was saved but in the 1st row id not the 2nd row id.

This is my view
Code:
<form method="post" action="<?php echo base_url()?>admin/requirements">
            <table cellpadding="0" cellspacing="0" border="0" width="100%"><tbody>
                <tr><td colspan="3"></td>
                    <td colspan="8" align="center"><font size="6px"></font>
                    R&nbsp;E&nbsp;Q&nbsp;U&nbsp;I&nbsp;R&nbsp;E&nbsp;M&nbsp;E&nbsp;N&nbsp;T&nbsp;S</td></tr>
                <th width="150">Names</th><th width="40" style="font-size: 10px;">Projects</th>
                <th width="40">Res</th><th width="40">Und</th><th width="40">WnP</th>
                <th width="40">MOA</th><th width="40">DTR</th><th width="40">Doc</th>
                <th width="40">Ref</th><th width="40">Pho</th><th width="40">Vid</th>
                &lt;?php
                    foreach($req_list->result() as $item){
                        echo "&lt;input type='hidden' name='student[]' value='".$item-&gt;student_id."'>";
                        echo "<tr><td>".$item->fullname."</td>";
                        echo "<td><a >g_id."'>Group ".$item->group_no."</a></td>";
                        echo "<td>&lt;input type='checkbox' name='res[]' value='1'&gt;&lt;/td>";
                        echo "<td>&lt;input type='checkbox' name='und[]' value='1'&gt;&lt;/td>";
                        echo "<td>&lt;input type='checkbox' name='wnp[]' value='1'&gt;&lt;/td>";
                        echo "<td>&lt;input type='checkbox' name='moa[]' value='1'&gt;&lt;/td>";
                        echo "<td>&lt;input type='checkbox' name='dtr[]' value='1'&gt;&lt;/td>";
                        echo "<td>&lt;input type='checkbox' name='doc[]' value='1'&gt;&lt;/td>";
                        echo "<td>&lt;input type='checkbox' name='ref[]' value='1'&gt;&lt;/td>";
                        echo "<td>&lt;input type='checkbox' name='pho[]' value='1'&gt;&lt;/td>";
                        echo "<td>&lt;input type='checkbox' name='vid[]' value='1'&gt;&lt;/td>";
                        
                        echo "</tr>";
                    }?&gt;
            </tbody></table>
            <div align="center">
            <button type="submit" class="button" id="deleteb"><div><b>&nbsp;&nbsp;Save Setting&nbsp;&nbsp;</b></div></button>
            &lt;input type="hidden" name="save" value="save"/&gt;&lt;/div>
            &lt;/form&gt;

My controller function
Code:
$save = $this->input->post('save');
        $student_id = $this->input->post('student');
        
        $resume = $this->input->post('res');
        $undertaking = $this->input->post('und');
        $waiver = $this->input->post('wnp');
        $moa = $this->input->post('moa');
        $dtr = $this->input->post('dtr');
        $documentation = $this->input->post('doc');
        $reflection = $this->input->post('ref');
        $photos = $this->input->post('pho');
        $videos = $this->input->post('vid');
        
        if($save)
        {
        for($i=0;$i<count($student_id);$i++)
        {
            $content[] = array(
                    'student_id' => $student_id[$i],
                    'resume' => $resume[$i]
                    'undertaking' => $undertaking[$i],
                    'waiver' => $waiver[$i],
                    'moa' => $moa[$i],
                    'dtr' =>  $dtr[$i],
                    'documentation' => $documentation[$i],
                    'reflection' => $reflection[$i],
                    'photos' => $photos[$i],
                    'videos' => $videos[$i]    
                );
        }
        

            foreach($content as $item=>$key)
            {
                #$this->admin_db->save_requirements($key['student_id'],$key);
                #redirect('admin/requirements');
                echo "<pre>";
                print_r($key);
                echo "</pre>";
            }
            #redirect('admin/requirements');
        }

Manage to have a picture. Pls see attach




Theme © iAndrew 2016 - Forum software by © MyBB