ajax变量未使用codeigniter

时间:2017-12-13 11:31:07

标签: ajax codeigniter

我正在将时间表转换为CSV文件。我需要使用user_idstart_dateend_date对其进行过滤。过滤后,它会将整个文件转换为CSV文件,但不包括过滤。

点击下载后,我已使用AJAX向我的控制器发送了start_dateend_date。但是,我只获得此类数据的空值。你能帮助我并告诉我为什么我的控制器中没有正确打印数据吗?

点击下载的AJAX代码是:

    function get_csv()
     {
        var user_id=$('#user').val();
        var start_date=$('#start_date').val();
        var end_date=$('#end_date').val();

        $.ajax({
            type: "POST",
            url: "<?php echo site_url();?>/timesheetmanager/timesheet/getcsv",
            data: '&start_date='+start_date+'&end_date='+end_date,
            success: function(data){
                document.location.href = '<?php echo site_url('timesheetmanager/timesheet/getcsv/'); ?>'+'/'+user_id;
            }
            });
     }

控制器:

    function getcsv($user_id)
        {       
            $userid=$user_id;   
            $start_date=$this->input->post('start_date');
            $end_date=$this->input->post('end_date');
            $this->db->where('id',$userid);
            $user_query=$this->db->get('users');
            $user_result=$user_query->row();
            if($userid != "0")
                {
                    $this->db->where('emp_id',$userid);
                }
                if(($start_date != 'Start Date')&& ($end_date != 'End Date'))
                    {
                        $startdate=@date('Y-m-d',strtotime($this->input->post('start_date')));
                        $enddate=@date('Y-m-d',strtotime($this->input->post('end_date')));
                        $datequery= $this->db->where("update_date BETWEEN '$startdate' and '$enddate'");        
                    }
            $header_name=array('TASK NAME','DESCRIPTION','HOURS SPEND','DATE');
            $this->db->order_by('id','desc');
            $query = $this->db->get('timesheet');
            $this->db->where('emp_id',$userid);
            $getRes = $query->result_array();
            $filename = ''.$name.date('Y-m-d').'.csv';
            header('Content-Type: text/csv; charset=utf-8');
            header('Content-Type: application/vnd.ms-excel'); 
            header("Content-Disposition: attachment; filename=$filename");
            $finalData[]=$user_result->name;
            $output = fopen('php://output', 'w');
            fputcsv($output,$header_name);
            $finalData = array();
            $fp = fopen('php://output','w');
            foreach( $getRes as  $data)
            {
                $finalData[]=$data['task_name'];
                $finalData[]=$data['description'];
                $finalData[]=$data['hours_spend'];
                $finalData[]=$data['update_date'];
                fputcsv($output, $finalData);
                unset($finalData);
            }
            fclose($fp);

        }

1 个答案:

答案 0 :(得分:1)

试试这个

 function get_csv()
 {
    var user_id=$('#user').val();
    var start_date=$('#start_date').val();
    var end_date=$('#end_date').val();

    $.ajax({
        type: "POST",
        url: "<?php echo site_url();?>/timesheetmanager/timesheet/getcsv/"+user_id,
        data: {'start_date': start_date,'end_date':end_date},
        success: function(data){
            document.location.href = '<?php echo site_url('timesheetmanager/timesheet/getcsv/'); ?>'+'/'+user_id;
        }
        });
 }