View Full Version : How to save multiple checkbox

08-23-2011, 06:18 PM
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.

<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>
<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>
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>";
<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>


08-23-2011, 06:28 PM
Make all checkbox names the same ...
and give them different values ...

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


Read it in as one array ...


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

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


08-23-2011, 06:38 PM
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

08-23-2011, 08:08 PM
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.

foreach($boxes as $item){

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?


08-23-2011, 08:28 PM
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?

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum