PHP不从ajax调用输出CSV

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");
duanmao1919
duanmao1919 哈哈@在名字,我的堂兄叫安东尼:)
5 年多之前 回复
dongshuo6185
dongshuo6185 github.com/johnculviner/jquery.fileDownload/blob/master/src/...
5 年多之前 回复
doutucui0133
doutucui0133 我建议你使用$.fileDownload();而不是使用$.aJax。
5 年多之前 回复
douhang5460
douhang5460 姓名,部门,“工作小时”,“假期”,生病,日期工作人员3,账户,7,是,2015-06-16“JoeBloggs”,审计,7,是,是的,2015-06-17“JoeBloggs“,Audit,7,yes,yes,2015-06-18”JoeBloggs“,Audit,4,yes,,2015-06-19”JoeBloggs“,Audit,6,,,2015-06-20“JoeBloggs”,审计,6,是的,2015-06-21“JoeBloggs”,审计,8,是的,2015-06-23“JohnSmith”,“营销和定价”,7,是的,2015-06-23name,Audit,3,,,2015-06-23“JoeBloggs”,Audit,7,,,2015-06-24“JoeBloggs”,Audit,7,,,2015-06-29“约翰史密斯”,“营销和定价”,8,,,2015-06-29staff3,Accounts,9,,,2015-06-29名称,审计,9.6,,,2015-06-29
5 年多之前 回复
douke1942
douke1942 是的,当我点击csv.php时,我会在DevTools响应窗口中返回输出。(@danny-broadbent你和我哥哥的名字相同:D)
5 年多之前 回复
douji0108
douji0108 您是否检查过任何控制台日志以查找从ajax调用返回的任何数据?
5 年多之前 回复

1个回答



将aJax更改为fileDownload:</ p>

  $。fileDownload('.. / report  /csv.php',{
httpMethod:'POST',
data:{
ajaxquery:ajaxquery,department:department,startdate:startdate,enddate:enddate,staffsearch:staffsearch
},
successCallback:function (url){
//插入成功代码

},
failCallback:function(html,url){
//插入失败代码
}
});
</ code> < / pre>

您可以通过此js文件使用jQuery fileDownload方法:
https://github.com/johnculviner/jquery.fileDownload/blob/master/src/Scripts/jquery.fileDownload.js </ p>
\ n

更多信息请访问:
http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-ric h-file-downloads / </ p>
</ div>

展开原文

原文

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/

duanba7498
duanba7498 大笑没问题,很高兴帮忙! :)我今天没有投票,所以直到明天才能发布你的帖子
5 年多之前 回复
duanhuantong8278
duanhuantong8278 哇,完美的工作。 我不想使用插件,因为CSV下载相当简单但不是这个。 我欠你一品脱:D。 我没有足够的代表upvote :(
5 年多之前 回复
立即提问