0

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

        }
2
  • timesheet is in what format? Excel? Commented Dec 13, 2017 at 11:33
  • 1
    Have a look here stackoverflow.com/a/5046951/578855 to see how to send post data Commented Dec 13, 2017 at 11:34

1 Answer 1

1

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;
        }
        });
 }
Sign up to request clarification or add additional context in comments.

1 Comment

When I disable (document.location.href = '<?php echo site_url('timesheetmanager/timesheet/getcsv/'); ?>'+'/'+user_id;) this line, the parameter values are passing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.