Javascript 使用jqueryajax生成报告

Javascript 使用jqueryajax生成报告,javascript,php,ajax,jquery,Javascript,Php,Ajax,Jquery,您好,我有一个jqueryajax接收动作的表单。 一旦从表单中提取数据,我就可以成功地调用laporan_per_bulan.php文件,该文件包含从ajax中提取数据并根据数据打印报告的代码 但是,报告没有出现? 我想要一个报告,当ajax调用success时,它会出现在一个新的选项卡中,比如在Google Chrome中,或者直接作为其他浏览器下载 这是我的ajax: $('#save_report').click(function(e){ ajax_cetak_laporan()

您好,我有一个jqueryajax接收动作的表单。 一旦从表单中提取数据,我就可以成功地调用laporan_per_bulan.php文件,该文件包含从ajax中提取数据并根据数据打印报告的代码

但是,报告没有出现? 我想要一个报告,当ajax调用success时,它会出现在一个新的选项卡中,比如在Google Chrome中,或者直接作为其他浏览器下载

这是我的ajax:

$('#save_report').click(function(e){
    ajax_cetak_laporan();
});

function ajax_cetak_laporan() {
    var bulan = $("#bulan option:selected").val();
    var kd_kelas = $("#kd_kelas option:selected").val();
    var kd_jur = $("#jur option:selected").val();

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "../bayarspp/main/page/laporan_per_bulan.php",
        data: "{'bulan':'" + bulan+ "', 'kd_kelas':'" + kd_kelas + "', 'kd_jur':'" + kd_jur + "'}",
        success: function (html) {
            // do something here...
        }
    });
}
这是我的laporan_per_bulan.php文件:

<?php
...
$bulan = $_POST['bulan']; 
$kd_kelas = $_POST['kd_kelas']; 
$jur = $_POST['kd_jur'];

$pdf->SetFont('Arial','',10);
$pdf->Cell(35, 0, 'Bulan');
$pdf->Cell(5, 0, ':');
$pdf->Cell(70, 0, $bulan);
$pdf->Ln(4);

$pdf->SetFont('Arial','',10);
$pdf->Cell(35, 3, 'Kelas');
$pdf->Cell(5, 3, ':');
$pdf->Cell(70, 3, $kd_kelas);
$pdf->Ln(10); 

#output file PDF
$pdf->Output();

?>


谢谢。

您可以根据ajax请求在服务器上生成并存储pdf,如“/folder\u name/uniqueID.pdf”

将uniqueID作为ajax响应返回(EXP:{“success:true”,“uniqueID:121212”})。然后写关于ajax成功部分的文章

  success: function(data) {
      if(data.success == true){                    
        window.location.href = site_url+path to pdf/+data.uniqueID.pdf;
      }
  }
或者您可以执行以下操作

success: function(data) {
   if(data.success == true){                    
     window.location.href  = site_url+path to pdf/download.php?uniqueID=data.uniqueID;
    }
  }
下载.php

if(isset($_REQUEST['uniqueID'])){
   $uniqueID= $_REQUEST['uniqueID'];
   $downname="downloading_filename.pdf";
   download($uniqueID,$downname);
}else{
    //return to any url or die;
}

function download($uniqueID,$downname){
        $cwd=getcwd();
        $filedestination = $cwd."/".$uniqueID.".pdf";

        if (file_exists($filedestination)) {    
                header('Content-Description: File Transfer');
                header('Content-type: application/pdf');
                header('Content-Disposition: attachment; filename='.$downname);
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($filedestination));
                ob_clean();
                flush();
                readfile($filedestination);
                ignore_user_abort(true);
                if (connection_aborted()) {
                        unlink($filedestination);
                }
                if(file_exists($filedestination)){
                        unlink($filedestination);
                }
                exit;
        }
}

请看这个jquery插件,它通过显示隐藏字段来完成这项工作

我在所有主要的浏览器上都查过了

此处的Url:

在ajax响应中无法执行此操作……响应类型是什么?html还是json?我不知道最好的响应类型是什么。但是我选择html怎么样?看起来你正在输出pdf;因此,我假设您的标题将设置为pdf?建议:不要通过ajax发布;为什么不作为一个普通的html表单发布呢?然后,在php中,您可以根据您的post值打印输出(在您的案例中为pdf格式)。