我有一个 php 脚本:
<?php
$species = $_GET['species']
...//do things to prepare the query
$result = //result of the query.
if (isset($_GET['download'])){
getCSV();
}
function getCSV(){
if ($result->num_rows > 0){
$f = fopen("csvFile", "w");
while ($row = $result->fetch_assoc()){
fputcsv($f, $row);
}
fclose($f);
echo ("downloaded");
}
else{
echo ("no results to download");
}
}
?>
还有一些html代码:
<button type="button" onclick="window.location.href='query.php?download=true'"> Click to download as csv file </button>
php 脚本和 html 代码都存在于同一个文件 query.php 中。php 脚本也是从另一个地方调用的,它向它发送“物种”GET 请求(以及其他几个,但我没有费心在此处包含它们)
我想在单击按钮时调用 getCSV 函数。我不希望页面刷新,所以我知道无论如何我都应该使用 ajax 和 jquery(并且知道以前有很多关于为此目的使用 ajax 的问题)。但是,php 脚本需要将$_GET['species']
和其他数据传递给它,因此当我单击按钮时(类似地,如果我要使用 ajax),'species' GET 变量会丢失,因为其他 GET 变量没有传递通过按钮查询.php。因此查询被错误地填充,并且 getCSV 函数被调用在无用的数据上。
getCSV() 函数是从 query.php 调用的。在调用时,我可以访问页面的 url,并对其进行操作以确定除“下载”(例如“物种”)之外的其他 GET 变量是什么。是否可以调用函数而不必这样做,因为我有很多变量?
提前致谢。