0

我有一个 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 变量是什么。是否可以调用函数而不必这样做,因为我有很多变量?

提前致谢。

4

1 回答 1

0

index.php看这里

<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<button type="button" id="test"> Click to download as csv file </button>
<input type="hidden" id="val" value="true"> <!--download filename value true and false-->

<script>
    $(function(){
        $('#test').click(function(){

            var val = $("#val").val();

            $.ajax({
                url: 'query.php',
                type: 'GET', // get method
                data: 'download='+val, // download get name + value go
                  success: function(data) {
                    // success
                    alert(data);
                  },
                  error: function(data) {
                    // error
                    alert(data);
                  }
            });
        });
    });
</script>

query.php看这里

<?php
$species = clean($_GET['download']); // clean func magic quotes fix

 if (empty($species)) {

     echo 'get name not empty';
        //  empty get name print

 }else{
    if ($species == 'true'){
        echo 'download';

        // true query code here...

    }else{
        echo 'no download';

        // error code here
    }
 }
?>

现场提供的文章样本的标题。在编写搜索之前。

我简单地告诉他,你可以调整你的系统。

问候。

于 2016-04-20T21:52:37.843 回答