1

I am doing a project for my school subject. I am confused on how to do checking data's in checkbox and when I press a submit button, it will loop to insert into my database. I manage to display/alert the data that is being checked in my data-table.

Here is my Contoller where it populates my data table:

public function getalldocs() {
    $listdocs = $this->Admin_model->getdoctors();
    $data = array();
    foreach ($listdocs as $docs) {
        $row = array();  
        $row[] = $docs->user_fname;
        $row[] = $docs->user_mname;
        $row[] = $docs->user_lname;
        $row[] = '<input name="user_id[]" value="'.$docs->user_id.'" type="checkbox">';

        $data[] = $row;
    }
    $output = array(   
        "data" => $data,
    );
    echo json_encode($output);
}

Here is in my view:

<div class="dataTable_wrapper">
    <table id="dataTables-docs"  class="table table-striped table-bordered table-hover dataTable dtr-inline" role="grid" style="width: 100%;" width="100%" aria-describedby="dataTables-material">
        <thead>
            <tr>
                <th>First Name</th>
                <th>Middle Name</th>
                <th>Last Name</th>
                <th></th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
</div><!-- dataTable_wrapper -->

here is my javascript to echo the selected check box from my data-table:

function show_docs() {
    $("#dataTables-docs").dataTable().fnDestroy();
    table =  $('#dataTables-docs').DataTable({
        "ajax": {
            "url": "<?php echo site_url('admin_controls/getalldocs')?>",
            "type": "POST",
        },
        responsive: true,
        className: 'select-checkbox',
        'bInfo': false,
        'paging': false
    });
}

$('#dataTables-docs tbody').on('click', 'input[type="checkbox"]', function(e){
    var user_id = $(this).val();
    alert(user_id);
});

now, i want to all that is being checked to be inserted in my database like this: (myid,selectedfromcheckbox); here is my screenshot from database table: enter image description here

1 Answer 1

2

Use another ajax to insert the data

$('#dataTables-docs tbody').on('click', 'input[type="checkbox"]', function(e){
    var user_id = $(this).val();
    $.ajax({
          type:"post",
          data: {user_id:user_id},
         "url": "<?php echo site_url('admin_controls/saveData')?>",
          success:function(data){
             $("#info").html(data);
          }

      });
});

// Below code in you controller

public function saveData()
{
// code to save in controler
}
Sign up to request clarification or add additional context in comments.

3 Comments

The function will be called once the check box is clicked.So no need of loop
what if i will uncheck? then need to update again? can you give me some code to alert if uncheck?
Hello .. How can i condition if the checkbox is checked or unchecked?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.