将两个变量从ajax传递给codeigniter时的混乱

时间:2018-08-23 18:21:21

标签: php ajax codeigniter

通过ajax将两个变量传递给控制器​​时,我有点困惑,它总是会失败,请帮助我。 这是我的模型功能

    public function get_dues_for_selected_month($studentID, $month){
    $this->db->select("student.studentID, invoice.invoiceID, student.name, 
    MONTHNAME(STR_TO_DATE(invoice.month, '%m')) as Month, invoice.year as year, 
    invoice.tuitionfee as 'tuitionfee', invoice.transportfee, 
    payment.tuitionpaid, payment.transportpaid, invoice.admissionfee, payment.admissionpaid", false);
    $this->db->from('invoice');
    $this->db->join('student','invoice.studentID = student.studentID');
    $this->db->join('payment','invoice.invoiceID = payment.invoiceID','LEFT');
    $this->db->where('invoice.`create_date` > DATE_SUB(now(), INTERVAL 6 MONTH) and 
    invoice.paidstatus != 2 and student.studentID=', $studentID);
    $this->db->where('Month' , $month);
    $this->db->group_by('invoice.month');
    $this->db->order_by('invoice.month', 'asc'); 
    $query = $this->db->get();
    // echo $this->db->last_query();
    // exit;
    return $query->result_array();
}

这是我的控制器功能

function get_dues_details_for_payment($studentID, $month){
        header('Content-Type: application/json; charset=utf-8');
        echo (json_encode($this->users_model->get_dues_for_selected_month($studentID, $month)));
    }

这是我的ajax功能

function selected_month_fee(){
    var dues_month = $('#fee_adj_month').val();
    var student_id = $('#student_id').val();
    var date = new Date(dues_month+' 1, 2000 00:00:00');
    var month_number = date.getMonth();
    console.log(month_number);
    $('#hid_details').show();
    console.log(dues_month);
    if(dues_month != ""){
        var post_url = "<?php echo base_url('inovice/get_dues_details_for_payment/');?>"+student_id+"/"+month_number;
        $.ajax({
            type: "POST",
            url: post_url,
            success: function(invoice){
                $.each(invoice,function(index,object)
                {
                    $('#due_adm_fee').val(object.admissionfee-object.admissionpaid); 
                    $('#due_tuition_fee').val(object.tuitionfee-object.tuitionpaid); 
                    $('#due_transport_fee').val(object.transportfee-object.transportpaid); 
                });
            }
        })}

请帮助某人。

4 个答案:

答案 0 :(得分:0)

您需要在ajax中添加数据

data: "{'data1':'" + value1+ "', 'data2':'" + value2+ "'}",

$.ajax({
            type: "POST",
            url: post_url,
            data: "{'data1':'" + value1+ "', 'data2':'" + value2+ "'}",
            success: function(invoice){
                $.each(invoice,function(index,object)
                {
                    $('#due_adm_fee').val(object.admissionfee-object.admissionpaid); 
                    $('#due_tuition_fee').val(object.tuitionfee-object.tuitionpaid); 
                    $('#due_transport_fee').val(object.transportfee-object.transportpaid); 
                });
            }
        })}

答案 1 :(得分:0)

您应该将变量用作发布数据,而不是将其用作url路径

答案 2 :(得分:0)

我想就是这样,并确保“ student_id”和“ month_number”不为空

function selected_month_fee(){
var dues_month = $('#fee_adj_month').val();
var student_id = $('#student_id').val();
var date = new Date(dues_month+' 1, 2000 00:00:00');
var month_number = date.getMonth();
console.log(month_number);
$('#hid_details').show();
console.log(dues_month);
if(dues_month != ""){
    var post_url = "<?php echo base_url('inovice/get_dues_details_for_payment/');?>"+student_id+"/"+month_number;
    $.ajax({
        type: "POST",
        url: post_url,
        // dataType: 'json', // <-- also you can enable dataType json if you need response in json
         data: { 
            'data1': 'valu1', 
            'data2': 'value2' 
        },
        success: function(invoice){
            $.each(invoice,function(index,object)
            {
                $('#due_adm_fee').val(object.admissionfee-object.admissionpaid); 
                $('#due_tuition_fee').val(object.tuitionfee-object.tuitionpaid); 
                $('#due_transport_fee').val(object.transportfee-object.transportpaid); 
            });
        }
    })}
}

答案 3 :(得分:0)

像这样简单地编写ajax:

$.ajax({
        url: "<?php echo base_url('inovice/get_dues_details_for_payment/');?>",
        type: "POST",
        data: {student_id: student_id,month_number:month_number},
        success: function(invoice){

        }
     });