使用 AJAX 和 PHP 的可下载 CSV 出现问题

Issue with downloadable CSV using AJAX with PHP

本文关键字:CSV 问题 下载 AJAX PHP 使用      更新时间:2023-09-26

>我正在尝试创建一个面板,我可以在其中从 5 个下拉列表中选择输入(4 个是多选下拉列表)并通过 ajax 调用发送它们。

在 ajax 函数中,我正在尝试创建一个 csv 可下载文件。

问题是,我可以收到警报以显示文件中应该包含的内容,但文件没有下载,也没有保存在某个文件夹中。

这是我触发 ajax 调用的 JavaScript 函数:

function create_csv()
{
    var v = $('#drp_v').val();
    var cnt = $('#drp_cnt').val();
    var ctg = $('#drp_ctg').val();
    var api = $('#drp_api').val();
    var nt = $('#drp_nt').val();
    alert("version :"+v+" category :"+ctg+" country :"+cnt);
    $.post("ajax.php",
            {   
                'version':v,'category':ctg,
                'country':cnt,'network_id':nt,
                'api':api,'func':'create_csv'
            },
            function(data)
            {
                alert(data);
            });
}

这是我的PHP函数

function create_csv($version,$ctg,$cnt,$nt,$api)
{
    $cnt_table = "aw_countries_".$version;
    $ctg_table = "aw_categories_".$version;
    $off_table = "aw_offers_".$version;

    $sizeof_ctg = count($ctg);
    $cond_ctg = " ( ";
    for($c = 0; $c < $sizeof_ctg ; $c++)
    {
        $cond_ctg = $cond_ctg." $ctg_table.category = '".$ctg[$c]."' ";
        if($c < intval($sizeof_ctg-1))
            $cond_ctg = $cond_ctg." OR ";
        else if($c == intval($sizeof_ctg-1))
            $cond_ctg = $cond_ctg." ) ";
    }
    $sizeof_cnt = count($cnt);
    $cond_cnt = " ( ";
    for($cn = 0; $cn < $sizeof_cnt ; $cn++)
    {
        $cond_cnt = $cond_cnt." $cnt_table.country = '".$cnt[$cn]."' ";
        if($cn < intval($sizeof_cnt-1))
            $cond_cnt = $cond_cnt." OR ";
        else if($cn == intval($sizeof_cnt-1))
            $cond_cnt = $cond_cnt." ) ";
    }
    $sizeof_nt = count($nt);
    $cond_nt = " ( ";
    for($n = 0; $n < $sizeof_nt ; $n++)
    {
        $cond_nt = $cond_nt." $off_table.network_id = '".$nt[$n]."' ";
        if($n < intval($sizeof_nt-1))
            $cond_nt = $cond_nt." OR ";
        else if($n == intval($sizeof_nt-1))
            $cond_nt = $cond_nt." ) ";
    }
    $sizeof_api = count($api);
    $cond_api = " ( ";
    for($a = 0; $a < $sizeof_api ; $a++)
    {
        $cond_api = $cond_api." $off_table.api_key = '".$api[$a]."' ";
        if($a < intval($sizeof_api-1))
            $cond_api = $cond_api." OR ";
        else if($a == intval($sizeof_api-1))
            $cond_api = $cond_api." ) ";
    }
    $output         = "";
    $sql = "SELECT $off_table.id,$off_table.name
            FROM $off_table,$cnt_table,$ctg_table
            WHERE  $off_table.id = $cnt_table.id
            AND $off_table.id = $ctg_table.id
            AND ".$cond_api."
            AND ".$cond_nt."
            AND ".$cond_cnt."
            AND ".$cond_ctg;

    $result = mysql_query($sql);
    $columns_total  = mysql_num_fields($result);
    // Get The Field Name
    for ($i = 0; $i < $columns_total; $i++) 
    {
        $heading    =   mysql_field_name($result, $i);
        $output     .= '"'.$heading.'",';
    }
    $output = trim($output,",");
    $output .="'n";
    while ($row = mysql_fetch_array($result)) 
    {
        for ($i = 0; $i < $columns_total; $i++) 
        {
            $output .='"'.$row["$i"].'",';
        }
        $output = trim($output,",");
        $output .="'n";
    }
    // Download the file
    $filename =  "myFile.csv";
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename='.$filename);
    echo $output;
    exit;
}

我需要进行哪些修改才能下载 CSV 文件?

也许会有点复杂:)您不能直接使用 ajax 下载 CSV。但是有一些技巧。

确保您的 mysql 连接在 ajax.php 中准备就绪。获取资源后,请随时创建一个隐藏表单,其中包含所需的所有数据

$( "body" ).append('    
 <form name="form" target="my_iframe">
  <input name ="version" value="'+v+'">
  <input name="country" value="'+cnt'+">
 <input name="api" value="'+api+'">');

等。。

像这样的东西,只需将此表单提交到隐藏的iframe之后

$('[name="form"]').submit();

仅此而已最后摧毁你的隐藏形式