ameya ganesan
ameya ganesan

Reputation: 3

ajax variable is not passing to controller using codeigniter

I am converting my timesheet to a CSV file. I need to filter it using user_id, start_date and end_date. After filtering, it converts the whole file to a CSV file but not including the filtering.

On clicking download, I have sent start_date and end_date using AJAX to my controller. However, I am getting only empty values for such data. Can you help me and suggest to me why the data is not printed correctly in my controller?

AJAX code on clicking download is:

    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;
            }
            });
     }

Controller :

    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);

        }

Upvotes: 0

Views: 164

Answers (1)

Artier
Artier

Reputation: 1673

try this

 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;
        }
        });
 }

Upvotes: 1

Related Questions