Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question

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
}
share|improve this answer
    
no loop? if there are many data selected? – Jc John Jan 19 at 6:31
    
The function will be called once the check box is clicked.So no need of loop – Nishant Nair Jan 19 at 6:33
    
what if i will uncheck? then need to update again? can you give me some code to alert if uncheck? – Jc John Jan 19 at 6:46
    
Hello .. How can i condition if the checkbox is checked or unchecked? – Jc John Jan 19 at 8:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.