3

I have a button that when clicked is supposed to submit variables to an ajax call which then a csv is created and downloaded but for some reason the file just isnt downloading. Yet I get the correct output in Chrome Dev tools:

Here is what I have:

index.php

<form class="navbar-form navbar-left" method="post">
<input hidden id="ajaxquery" value="<?php echo $ajaxquery;?>">
<button type="button" class="btn btn-success btn-lg" id="downloadcsv">Download CSV</button>
</form>

script.js

$(document).ready(function() {
var csvquery = function(){
    function getUrlParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}     
    ajaxquery = $('#ajaxquery').val();
    department = getUrlParameter('department');
    startdate = getUrlParameter('startdate');
    enddate = getUrlParameter('enddate');
    staffsearch = getUrlParameter('staffsearch');
                $.ajax({
                    type: 'POST', // type
                    url: '../report/csv.php', // request file the 'check_email.php'
                    data: {ajaxquery:ajaxquery, department: department, startdate:startdate, enddate: enddate, staffsearch: staffsearch},
                    success: function(responseText) {

    }
                    }); // end success
            }
$('#downloadcsv').click(csvquery);
});

csv.php

session_start();
require '../connect.php';
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen('php://output', 'w');

fputcsv($output, array('Name', 'Department','Hours Worked', 'On Holiday', 'Ill' , 'Date'));

$sql = "SELECT time.id as timeid, time.staff_id, time.timein, time.onholiday, time.dateadded, time.ill, time.notes, staff.id AS staffid, department.id AS departmentid, department.department_name, staff.staff_name, staff.department_id FROM time, staff, department WHERE staff.id = time.staff_id AND staff.department_id = department.id ORDER BY `time`.`dateadded` ASC ;";

$rows = mysqli_query($connect, $sql);
while ($rowcsv = mysqli_fetch_assoc($rows)){ 
    fputcsv($output, array($rowcsv['staff_name'],$rowcsv['department_name'],$rowcsv['timein'],$rowcsv['onholiday'],$rowcsv['ill'],$rowcsv['dateadded']));
};
readfile("php://output");
6
  • Have you checked any console logs for any data coming back from the ajax call? Commented Jun 30, 2015 at 13:50
  • Yes I get the output coming back in Dev Tools response window when I click on csv.php. (@danny-broadbent you have the same name as my brother :D) Commented Jun 30, 2015 at 13:53
  • 1
    Name,Department,"Hours Worked","On Holiday",Ill,Date staff3,Accounts,7,yes,,2015-06-16 "Joe Bloggs",Audit,7,yes,yes,2015-06-17 "Joe Bloggs",Audit,7,yes,yes,2015-06-18 "Joe Bloggs",Audit,4,yes,,2015-06-19 "Joe Bloggs",Audit,6,,,2015-06-20 "Joe Bloggs",Audit,6,yes,,2015-06-21 "Joe Bloggs",Audit,8,,yes,2015-06-23 "John Smith","Marketing and Pricing",7,yes,,2015-06-23 name,Audit,3,,,2015-06-23 "Joe Bloggs",Audit,7,,,2015-06-24 "Joe Bloggs",Audit,7,,,2015-06-29 "John Smith","Marketing and Pricing",8,,,2015-06-29 staff3,Accounts,9,,,2015-06-29 name,Audit,9.6,,,2015-06-29 Commented Jun 30, 2015 at 13:53
  • 2
    Rather than using $.aJax i would suggest you use $.fileDownload(); Commented Jun 30, 2015 at 13:55
  • github.com/johnculviner/jquery.fileDownload/blob/master/src/… Commented Jun 30, 2015 at 13:56

1 Answer 1

5

Change aJax to fileDownload:

$.fileDownload('../report/csv.php', {
    httpMethod: 'POST',
    data: {
        ajaxquery:ajaxquery, department: department, startdate:startdate, enddate: enddate, staffsearch: staffsearch
    },
    successCallback: function (url) {
        //insert success code

    },
    failCallback: function (html, url) {
        //insert fail code
    }
});

You can use jQuery fileDownload method through this js file: https://github.com/johnculviner/jquery.fileDownload/blob/master/src/Scripts/jquery.fileDownload.js

More information at: http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/

Sign up to request clarification or add additional context in comments.

2 Comments

Wow that works perfectly. I didnt want to use plugins as CSV downloads are fairly simple but not this one. I owe you a pint :D. I dont have enough rep to upvote :(
Lol no problem, glad it helped! :) im out of votes today, so can't up your post until tomorrow

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.