多行插入到codeigniter中的db中

时间:2015-10-23 15:34:55

标签: ajax codeigniter

ajax更改函数执行listByBatch()并按批次显示包含数据的表,该数据由ajax发布。然后我尝试按insertAttendance插入表格行,代码如下:

 Jquery post:

    $("#batch-list").change(function(){
                /*dropdown post */
                $.ajax({
                url:"<?php echo base_url(); ?>index.php/attendance/list_ByBatch",    
                data: {batchid: $(this).val()},
                type: "POST",
                dataType:'json',
                success: function(data){ 
                    $("#traineeList").html(data);
                    $("#subTotal").html("Total: " + $('#traineeList tr').length.toString());
                    document.getElementById("classHour").defaultValue = "4";
                }
                });
             });

Controller:

    public function list_ByBatch() {
        $batch = $this->input->post('batchid', TRUE);
        $data['trainee']= $this->AttendanceModel->get_traineeList($batch);
        if (!empty($data['trainee'])) {
            echo form_open('attendance/insertAttendance');
                echo"<table class='table table-hover type-list2'id='traineeList'>
                    <tr class='success'>
                        <th>Trainee ID</th>
                        <th>Trainee Name</th>
                        <th>Present</th>
                    </tr>";
                foreach ($data['trainee'] as $row) {
                    echo "
                    <tr><td>" . str_pad($row->TraineeID, 7, "0", STR_PAD_LEFT) . "</td>
                        <td>" . $row->Name . "</td>
                        <td><input type='checkbox' name='.$row->TraineeID[]' value='" . $row->TraineeID . "' checked></td>
                    </tr>";
                }
                echo"</table>";
                echo"
                <div class='row'>
                    <div class='form-group col-sm-2'>
                        <h5 id='subTotal'></h5>
                    </div>
                    <div class='form-group col-sm-6'>
                        <input type='date' class='form-control' name='attnDate' id='attnDate' placeholder='Attendance Date' required>
                    </div>
                    <div class='form-group col-sm-2'>
                        <input type='number ' class='form-control' name='classHour' id='classHour' placeholder='Class Hour' required>
                    </div>
                    <div class='form-group col-sm-2'>
                        <input type='submit' class='btn btn-default btn-success' name='record' value='Record'>
                    </div>
                </div>";
            echo "</form>";
        }
    }

    public function insertAttendance() {
        $TID ['TID']= $this->input->post('trainee');
        $attnDate = $this->input->post('attnDate');
        $classHour = $this->input->post('classHour');
        echo '<pre>';
        print_r($TID);

        if(is_array($TID)){
            foreach ($TID as $TID=>$key) {
                $query = "INSERT INTO `tbl_attn_temp` (TraineeID, Date, classHour) VALUES ('" . $key . "','" . $attnDate . "','" . $classHour . "')";
                $this->db->query($query);
            }
        }else{
            echo "Trainee ID is not array";
        }

    }

我只是尝试在表中插入所有行(TraineeID)显示&#34; attnDate&#34;和&#39; classHour&#34;点击提交按钮。

0 个答案:

没有答案