可下载的 CSV 文件通过 Ajax
Downloadable CSV file Via Ajax
如果我成功使用 Ajax,我无法弄清楚如何获取可下载的 csv 文件。我将 link js ajax 脚本以及在 Ajax 函数中调用的我的 php 文件。感谢您的帮助。我正在 return 回到我的 Ajax 函数的 Success 函数。我只是不知道如何将我的数据 return 作为可下载的 csv 文件。
JS函数:
function popupClick2 (){
var popupObj2 = {};
var c = '0';
var p = '0';
var i = '0';
if (document.getElementById('checkboxC').checked){c = '1'}
if (document.getElementById('checkboxP').checked){p = '1'}
if (document.getElementById('checkboxI').checked){i = '1'}
popupObj2["checkboxC"] = c;
popupObj2["checkboxP"] = p;
popupObj2["checkboxI"] = i;
popupObj2["rangeD"] = $('#rangeD').val();
popupObj2["year"] = $('#year').val();
popupObj2["popupObj"] = '2';
$.ajax({
type: "POST",
dataType: "text",
url: "popupAjax.php",
data: popupObj2,
cache: false,
success: function(data)
{
alert("Success");
//I would like to have the csv file downloadable here.
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
closePopup2();
}
PHP (popupAjax.php)
<?php
$weekEnding = '';
$PHSN = '';
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=eWFO-Report.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Week Ending', 'WN', 'Project Title', 'Project Contact',
'Org No', 'PHSN', 'No', 'Verified By', 'Date Verified',
'Comments', 'Notes'));
/*** connect to SQL DB ***/
$dbe = get_db_connection('db');
$dbe->connect();
/*** connect or Oracle DB ***/
$db = oci_connect('query','pw','server:1521/world');
if (!$db){
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$query = "SELECT * FROM db.dbstuff WHERE (STATUS = 'ACTIVE' OR STATUS = 'CLOSED') AND NUMBER <> ' '";
$runQuery = oci_parse($db, $query);
oci_execute($runQuery);
while($row = oci_fetch_array($runQuery, OCI_ASSOC+OCI_RETURN_NULLS))
{
$WFON = $row['NUMBER']."-".$row['ANUMBER'];
$querySQLDB = "SELECT [Verified_By], [Comments], [Notes], [Date_Verified]
FROM dbo.Information
WHERE dbo.Information.Key_ID = '$WFON'
ORDER BY dbo.Information.ID DESC";
$dbe->query($querySQLDB);
$sqlData = $dbe->fetch();
$dateNoTime = str_replace("12:00:00:000AM"," ",$sqlData['Date_Verified']);
fputcsv($output, array($weekEnding, $WFON, $row['TITLE'], $row['NAME'],
$row['ORG'], $PHSNumber, $sqlData['Verified_By'], $dateNoTime,
$sqlData['Comments'], $sqlData['Notes']));
}
echo $output;
?>
您可以在 popupClick2 函数中即时添加表单,例如:
function popupClick2 (){
...
($('<form/>', {
'id': 'tmpCsvForm',
'action': "popupAjax.php",
'method': 'post'
}).append($('<input />', {
'type': 'hidden',
'name': 'data',
'value': popupObj2
}))).appendTo('body');
$('form#tmpCsvForm').submit().remove();
}
如果我成功使用 Ajax,我无法弄清楚如何获取可下载的 csv 文件。我将 link js ajax 脚本以及在 Ajax 函数中调用的我的 php 文件。感谢您的帮助。我正在 return 回到我的 Ajax 函数的 Success 函数。我只是不知道如何将我的数据 return 作为可下载的 csv 文件。
JS函数:
function popupClick2 (){
var popupObj2 = {};
var c = '0';
var p = '0';
var i = '0';
if (document.getElementById('checkboxC').checked){c = '1'}
if (document.getElementById('checkboxP').checked){p = '1'}
if (document.getElementById('checkboxI').checked){i = '1'}
popupObj2["checkboxC"] = c;
popupObj2["checkboxP"] = p;
popupObj2["checkboxI"] = i;
popupObj2["rangeD"] = $('#rangeD').val();
popupObj2["year"] = $('#year').val();
popupObj2["popupObj"] = '2';
$.ajax({
type: "POST",
dataType: "text",
url: "popupAjax.php",
data: popupObj2,
cache: false,
success: function(data)
{
alert("Success");
//I would like to have the csv file downloadable here.
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
closePopup2();
}
PHP (popupAjax.php)
<?php
$weekEnding = '';
$PHSN = '';
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=eWFO-Report.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Week Ending', 'WN', 'Project Title', 'Project Contact',
'Org No', 'PHSN', 'No', 'Verified By', 'Date Verified',
'Comments', 'Notes'));
/*** connect to SQL DB ***/
$dbe = get_db_connection('db');
$dbe->connect();
/*** connect or Oracle DB ***/
$db = oci_connect('query','pw','server:1521/world');
if (!$db){
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$query = "SELECT * FROM db.dbstuff WHERE (STATUS = 'ACTIVE' OR STATUS = 'CLOSED') AND NUMBER <> ' '";
$runQuery = oci_parse($db, $query);
oci_execute($runQuery);
while($row = oci_fetch_array($runQuery, OCI_ASSOC+OCI_RETURN_NULLS))
{
$WFON = $row['NUMBER']."-".$row['ANUMBER'];
$querySQLDB = "SELECT [Verified_By], [Comments], [Notes], [Date_Verified]
FROM dbo.Information
WHERE dbo.Information.Key_ID = '$WFON'
ORDER BY dbo.Information.ID DESC";
$dbe->query($querySQLDB);
$sqlData = $dbe->fetch();
$dateNoTime = str_replace("12:00:00:000AM"," ",$sqlData['Date_Verified']);
fputcsv($output, array($weekEnding, $WFON, $row['TITLE'], $row['NAME'],
$row['ORG'], $PHSNumber, $sqlData['Verified_By'], $dateNoTime,
$sqlData['Comments'], $sqlData['Notes']));
}
echo $output;
?>
您可以在 popupClick2 函数中即时添加表单,例如:
function popupClick2 (){
...
($('<form/>', {
'id': 'tmpCsvForm',
'action': "popupAjax.php",
'method': 'post'
}).append($('<input />', {
'type': 'hidden',
'name': 'data',
'value': popupObj2
}))).appendTo('body');
$('form#tmpCsvForm').submit().remove();
}