-1

我正在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>

4

1 回答 1

2

您可以做的是添加两个复选框作为存在和不存在然后遍历它们中的每一个并插入数据

此外,如果您想要单个复选框,那么您可以将 id_(number) 作为复选框名称并以相同的方式发送用户 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
         }
}
于 2013-11-03T06:51:42.620 回答