Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to save multiple checkbox

    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.

    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>
                    <?php 
                        foreach($req_list->result() as $item){ 
                            echo "<input type='hidden' name='student[]' value='".$item->student_id."'>";
                            echo "<tr><td>".$item->fullname."</td>";
                            echo "<td><a href='".base_url()."admin/projects_view/".$item->g_id."'>Group ".$item->group_no."</a></td>";
                            echo "<td><input type='checkbox' name='res[]' value='1'></td>";
                            echo "<td><input type='checkbox' name='und[]' value='1'></td>";
                            echo "<td><input type='checkbox' name='wnp[]' value='1'></td>";
                            echo "<td><input type='checkbox' name='moa[]' value='1'></td>";
                            echo "<td><input type='checkbox' name='dtr[]' value='1'></td>";
                            echo "<td><input type='checkbox' name='doc[]' value='1'></td>";
                            echo "<td><input type='checkbox' name='ref[]' value='1'></td>";
                            echo "<td><input type='checkbox' name='pho[]' value='1'></td>";
                            echo "<td><input type='checkbox' name='vid[]' value='1'></td>";
                            
                            echo "</tr>";
                        }?>
                </tbody></table>
                <div align="center">
                <button type="submit" class="button" id="deleteb"><div><b>&nbsp;&nbsp;Save Setting&nbsp;&nbsp;</b></div></button>
                <input type="hidden" name="save" value="save"/></div>
                </form>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,470
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Make all checkbox names the same ...
    and give them different values ...

    name='checkbox[]' value='res'
    name='checkbox[]' value='und'
    name='checkbox[]' value='wnp'

    etc.

    Read it in as one array ...

    $boxes=$_POST['checkbox'];

    Loop through ... the array will only contain the ones that are checked ...

    foreach($boxes as $item){
    echo $item."<br />";
    }



    .

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    First of all thanks for the reply.

    I can't do that, since I'm pulling the data from my db. The rows in the table will contain at least 40 people with each 9 checkbox. Also the value must only be 1 or 0.

    The module that I'm working right now is a Requirement Checker

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,470
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    I'm not seeing the problem ...

    To process the checkbox form:
    make the values the name of your columns ...

    name='checkbox[]' value='res'
    name='checkbox[]' value='und'
    name='checkbox[]' value='wnp'

    When you do the table update, default them all to 0, then loop through
    the checkbox[] array. Any of the values that appear (checkbox checked),
    set them to a 1.

    $boxes=$_POST['checkbox'];
    $res=0;
    $und=0;
    $wnp=0;
    foreach($boxes as $item){
    $$item=1;
    }

    note: $$item (double $$) is deliberate.
    "variable of a variable" ... so if the value of $item is "wnp", $wnp will be changed to a value of 1.

    If your concern is reading the table and creating the form (with appropriate ones checked),
    that's a different process.

    Is your problem creating the form, or processing the form?


    .
    Last edited by mlseim; 08-23-2011 at 07:11 PM.

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ok, got your idea. Now how can I point the id of the row since I am updating the data in my table. My id is student_id and as you can see to my original post I've set it hidden in my input. Is it right?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •