2015-06-30 13:49 阅读 111


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:


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


$(document).ready(function() {
var csvquery = function(){
    function getUrlParameter(sParam)
    var sPageURL =;
    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');
                    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


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 as timeid, time.staff_id, time.timein, time.onholiday, time.dateadded, time.ill, time.notes, AS staffid, AS departmentid, department.department_name, staff.staff_name, staff.department_id FROM time, staff, department WHERE = time.staff_id AND staff.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']));
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新