'How to send an array data through AJAX and retrieve the data in CodeIgniter

I wanted to send a table employee id through ajax and retrieve data from the database. The SQL code I used is working. I have tested it. The mistake I'm doing is in the process of sending the employee id array and the way of retrieving the data according to my knowledge.

This is my code in the view

function getAmounts() {

    var t_of_allowance = $("#t_of_allowance").val();
    var tableSize = $("#DataTables_Table_0 tbody tr").length;
    document.getElementById('rowCount').value = tableSize;
    const emp_id_tbls_array = [];

    var l =0;
    for (var k=0; k<tableSize; k++) {

        var emp_id_tbls = $.trim($('#emp_id' + k).val());
        $('#amount'+k).val('0.00');
        
        emp_id_tbls_array[k] = emp_id_tbls;
    }
    
    $.ajax({
        type: 'post',
        url: "<?PHP echo base_url("Activi_allowances_con/get_amounts");?>",
        data: {
            t_of_allowance: t_of_allowance,
            emp_id_tbls_array: emp_id_tbls_array
        },
        success: function (data) {

            var json_value = JSON.parse(data);
            console.log(json_value);
            if (json_value[0] != null) {

                document.getElementById("amount" + k).value = accounting.formatNumber(json_value[0]['amount'], 2, ",");

            } else {
                document.getElementById("amount" + k).value = 0.00;
            }
        }
    });

}

This is in the controller

public function get_amounts(){

    $t_of_allowance =$this->input->post('t_of_allowance');
    $emp_id_tbls_array =$this->input->post('emp_id_tbls_array');

    for($i=0; $i<count($emp_id_tbls_array); $i++){
        $result[$i] = $this->Activi_allowances_model->get_amounts($t_of_allowance,$emp_id_tbls_array[$i]);
    }
    
    if ($result!=''){
        echo json_encode($result);
    }else{
        echo 'NV';
    }
}

This is the model

public function get_amounts($t_of_allowance,$emp_id_tbls){

    $rs='';

    if ($t_of_allowance!='All') {
        $rs = $this->db->query("SELECT empAttendance_daily_New.*,attendance_allowances.amount 
                                FROM (
                                    SELECT SUM(empAttendance_daily.full + (empAttendance_daily.half)/2) as approved_days
                                    FROM empAttendance_daily WHERE empId='$emp_id_tbls' AND SUBSTRING(empAttendance_daily.year_month, 6, 7) = date('m')
                                ) empAttendance_daily_New
                                LEFT JOIN attendance_allowances ON attendance_allowances.from <= empAttendance_daily_New.approved_days AND attendance_allowances.to >= empAttendance_daily_New.approved_days AND attendance_allowances.name = '$t_of_allowance' AND attendance_allowances.active = 1")->result();
        return $rs;

    }else{
        return $rs;
    }

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source