lundi 7 décembre 2020

Insert multiple checkbox from datatable codeigniter ajax

I want to insert multiple file from library table, that table using datatable. What I want to ask is if I choose data on page 1 and page 2, the only data insert to database only data on page 1. I've sent data using ajax to controller, and in the controller, I using json_encode to send response

Do you know how to fixed this ?

Here's the view code:-

<form id="f_data2">
<!-- ID -->
<input type="hidden" name="college_id" class="college_id">
<input type="hidden" name="courses_id" class="courses_id">

    <div class="table-responsive">
        <table class="table table-hover dataTables">
            <thead class="thead-light">
                <th width="5%">Choose</th>
                <th width="20%">Document Type</th>
                <th width="20%">Document Format</th>
                <th width="30%">Document Title</th>
                <th width="25%">Description</th>
            </thead>
            <tbody>
                <?php $no=1;foreach($library as $libs): ?>
                <tr>
                    <td>
                         <input type="checkbox" name="material[]" value="<?=$libs->id?>" >
                    </td>
                    <td><?=$libs->name?></td>
                    <td>
                        <?php if($libs->icon != ''): ?>
                            <i class="fas <?=$libs->icon;?>"></i>
                        <?php endif;?>
                    </td>
                    <td><?=$libs->title?></td>
                    <td><?=$libs->description?></td>
                </tr>
                <?php endforeach;?>
            </tbody>
        </table>
    </div>
    <div class="col-sm-4" style="margin-right: -110px !important;"> 
        <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i>Save</button>   
    </div>

Here's the jQuery code:-

$('form#f_data2').submit(function(e){
      e.preventDefault();
      var formData = new FormData(this);
      $.ajax({
          url : url + 'Material/save_from_lib',
          type: 'POST',
          data: formData,
          success: function (data) {
              var jsonData = JSON.parse(data);
              var material_id = jsonData.material_id;

              if(jsonData.success){

                alert("Success");
                var direct = "url";
                location.href = url + direct;

                // location.reload();

              }else{
                alert('Failed');
              }
          },
          cache: false,
          contentType: false,
          processData: false
      });
    });

Here's the controller code:-

 public function save_from_lib(){

    $_id = $this->input->post('college_id');
    $time= $this->input->post('time');

    $material = $this->input->post('material');
    if(isset($material)){
        foreach ($material as $key => $material_id) {
            $data['college_id']  = $college_id;
            $data['material_id'] = $material_id;
            $data['start_time']   = $time;
            $data['created_by']    = $this->session->userdata('user_id');
            $this->model->insert($data);
        }

        $result['success'] = true;
        $result['college_id'] = $college_id;
    }else{
        $result['success'] = false;
    }

    echo json_encode($result);
}



Aucun commentaire:

Enregistrer un commentaire