gpt4 book ai didi

javascript - 通过php、ajax和mysql创建CSV文件

转载 作者:行者123 更新时间:2023-11-29 09:32:19 24 4
gpt4 key购买 nike

我有以下问题:

我正在升级一个工作系统,所以我必须创建适合的代码。我想在 php 页面和 mysql 数据库上创建一个 csv 文件。我还使用 Ajax 在运行其他 php 文件时保持在同一页面上。以下是代码片段:

带按钮的 PHP/HTML 页面

<div class="btn" id="export">Export</div>

Javascript Ajax

$("#export").click(function() {exportInfos();});

function exportInfos() {
$.ajax({
type: "POST",
url: "functions/exportInfos.php",
data: { searchterm: $("#search").val(), filterbycat: $("#filterbycat").val(), filterbytype: $("#filterbytype").val()},
success: function(response){
console.log("success_export");
},
});
}

用于创建 csv 的 PHP 文件(exportInfos.php):

<?php
include ('../../config.php');

$searchterm = $_POST["searchterm"];
$filterbycat = $_POST["filterbycat"];
$filterbytype = $_POST["filterbytype"];

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=export.csv');
$output = fopen("php://output", "w");
$query = "SELECT * FROM database";
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($output, $row);
}
fclose($output);
?>

我不确定问题出在哪里,但在控制台中我只看到调用了 php 脚本以及日志中的 success_export 文本,但没有打开或下载任何文件。我认为问题可能出在 AJAX 部分,因为那是我最不确定的部分。

一旦我得到一些输出文件,Ajax 部分中的数据值就可以编辑查询。$mysqli 是配置文件中定义的连接。

最佳答案

我认为您需要更改下载方法。也许可以是这样的。

静态页面:

<!doctype html>
<html lang="en">
<body>
<div class="btn" id="export">Export</div>
</body>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$('#export').click(function() {exportInfos();});

function exportInfos() {
$.ajax({
type: 'POST',
url: 'functions/createCSV.php',
data: {
searchterm: $('#search').val(),
filterbycat: $('#filterbycat').val(),
filterbytype: $('#filterbytype').val(),
},
success: function(response) {
if (response.filename != undefined && response.filename.length > 0) {
window.open('functions/downloadCSV.php?filename='+response.filename);
}
},
});
}
</script>
</html>

创建CSV:

<?php

include('../../config.php');

$searchterm = $_POST["searchterm"];
$filterbycat = $_POST["filterbycat"];
$filterbytype = $_POST["filterbytype"];

//header('Content-Type: text/csv');
//header('Content-Disposition: attachment; filename=export.csv');

$outputPath = '/path/to/save/outputFile.csv';

$output = fopen($outputPath, "w");
$query = "SELECT * FROM database";
$result = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_assoc($result)) {
fputcsv($output, $row);
}
fclose($output);

header('Content-Type: application/json');
echo json_encode(
[
"filename" => basename($outputPath),
]
);

下载CSV:

<?php

if (!empty($_GET['filename'])) {
http_send_status(404);
}

$filepath = "/path/to/save/" . $_GET['filename'];

if (!is_file($filepath) || !is_readable($filepath)) {
http_send_status(404);
}

header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=" . $_GET['filename']);

echo file_get_contents($filepath);

关于javascript - 通过php、ajax和mysql创建CSV文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58433572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com