使用codeigniter中的ajax使用切换开关进行数据库更新

时间:2015-12-31 11:37:34

标签: php jquery ajax codeigniter checkbox

我正在开发一个考勤管理codeigniter应用程序,我正在使用切换开关插件来更新出勤状态。这个过程将是:

  1. 系统显示具有出勤状态的学生列表,带有可切换插件的复选框。
  2. 如果学生在场或缺席或只是用户更改状态,请使用选中或取消选中带有可切换插件的复选框。
  3. 系统使用AJAX发送表格,如果学生出现1或缺席0,学生证和出勤日期。
  4. 简单的系统会改变学生的出勤状态。
  5. 注意* $ date $ month $ year $ program_id $ batch_id < / strong>和 $ section_id 在控制器函数参数之前加载。
  6. 这是我应用的预览。我需要使用AJAX这个表单。

    preview of attendance app with switchery plugin

    这是我的观点代码:

    &#13;
    &#13;
    <!-- student attendence status update form -->
    <form method="post" action="<?php echo base_url(); ?>index.php?admin/student_attendance">
      <!-- start datatable -->
      <table class="table table-hover dataTable table-striped width-full" id="departmentTableTools">
        <thead>
          <tr>
            <th class="text-center"><?php echo get_phrase('rool'); ?></th>
            <th class="text-center"><?php echo get_phrase('name'); ?></th>
            <th class="text-center"><?php echo get_phrase('status'); ?></th>
          </tr>
        </thead>
        <tbody>
          <?php
    foreach($students as $row):?>
          <tr>
            <td class="text-center"><?php echo $row['roll_no'];?></td>
            <td class="text-center"><?php echo $row['name'];?></td>
            <td class="text-center">
              <input type="checkbox" value="1" name="status_<?php echo $row['student_id'];?>" data-plugin="switchery" <?php if($status == 1) echo 'checked'; ?>/>
            </td>
          </tr>
          <?php endforeach;?>
        </tbody>
      </table>
      <input type="hidden" name="date" value="<?php echo $full_date;?>" />
    </form>
    <!-- end the attendance status update form -->
    &#13;
    &#13;
    &#13;

    这是我的控制器代码:

    &#13;
    &#13;
    if($_POST) // if the update request is send
    {
    foreach ($students as $row)
    {
    $this->crud_model->update_student_attendance($students['student_id'], $this->input->post('status_' . $row['student_id']), $this->input->post('date'));
    }
    $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
    redirect(base_url() . 'index.php?admin/student_attendance/'.$date.'/'.$month.'/'.$year.'/'.$program_id.'/'.$batch_id , 'refresh');
    }
    &#13;
    &#13;
    &#13;

    这是我更新的模型代码和db值:

    &#13;
    &#13;
    function update_student_attendance($student_id, $attendance_status, $full_date){
    $this->db->where('student_id' , $student_id);
    $this->db->where('date' , $full_date);
    $this->db->update('attendance' , array('status' => $attendance_status));
    }
    &#13;
    &#13;
    &#13;

0 个答案:

没有答案