如何获取codeigniter中复选框的未选中值

时间:2013-11-03 04:13:45

标签: php html codeigniter input checkbox

我正在开发codeigniter的考勤系统。有一个复选框列表。我成功地将已检查的数据插入到数据库中,但我想知道如何将未经检查的数据插入数据库,以及如何检测是否检查了盒子是否与java一样。

这是我的观点

<table align="center">
<tr>
    <th><input type="checkbox" title="Select all" onclick="changeAll(this.checked);"/></th>
    <th>student id</th>
    <th>student name</th>
    <th>class</th>
    <th>section</th>
</tr>
<form action="<?php echo base_url();?>index.php/student/attendance/submit" onsubmit="return checkForm();" method="post">
<?php foreach($result as $r):?>
<tr>
    <td><input type="checkbox" name="id[]" id="id[]" value="<?php echo $r->sid ?>"/></td>
    <td><?php echo $id=$r->sid ?></td>
    <td>
        <?php 
            $this->db->where('id',$id);
            $query=$this->db->get('student_basic_info');
            foreach($query->result() as $re){
                echo $re->f_name." ".$re->l_name;
            }
        ?>
    </td>
    <td><?php echo $r->class_name?></td>
    <td><?php echo $r->section?></td>
</tr>
<?php endforeach;?>
<tr class="no">
    <td class="no"><input type="submit" value="Submit" name="action"></td>
</tr>
    </form>

1 个答案:

答案 0 :(得分:2)

你可以做的是添加两个复选框作为存在和不存在 然后循环遍历每个数据并插入数据

另外 如果你想要单一的复选框 您可以将id_(数字)作为复选框名称,并以相同的方式发送用户ID的隐藏值。最后将用户的总数发送为隐藏的太多。

<?php 
$i = 1;
foreach($result as $r):?>
<tr>
    <td><input type="checkbox" name="id_$i" id="id_$i" value="<?php echo $r->sid ?>"/></td>
    <td><?php echo $id=$r->sid ?></td>
    <td>
        <?php 
            $this->db->where('id',$id);
            $query=$this->db->get('student_basic_info');
            foreach($query->result() as $re){
                echo $re->f_name." ".$re->l_name;
            }
        ?>
    </td>
    <td><?php echo $r->class_name?></td>
    <td><?php echo $r->section?></td>
</tr>
//Here is the user id 
<input type="hidden" name="user_$i" value="your user id">
<?php

$i++;
endforeach;?>

//now send the total users again in hidden field
<input type="hidden" name="totalusers" value="<?php echo $i; ?>">


//NOw in our php script

for($i=1;$i<=$_POST['totalusers'];$i++){
     if(isset($_POST["id_$i"]))
          //this means the user is present add it to database as your requirement
     else{
         //this means the checkbox was not checked so 
         //for this we get the unchecked users id from $_POST["user_$i"]
         //now add to the database for this user as absent
         }
}