2013-09-23 44 views
0

您好我有一個jQuery Ajax接收窗體的操作。 根據數據,一旦表單I中的數據可以成功調用laporan_per_bulan.php文件,該文件包含從Ajax中提取數據並打印報表的代碼。使用JQuery生成報告Ajax

但是,報告沒有出現? 當我想要一個報告出現在谷歌瀏覽器中的新標籤中,或者在ajax調用成功時直接作爲其他瀏覽器下載。

這是我的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(); 

?> 

謝謝。

+0

不能在Ajax響應做到這一點..... –

+0

什麼是響應類型? HTML或JSON? –

+0

我不知道什麼是最好的響應類型。但如何選擇HTML? – fanjavaid

回答

0

您可以在ajax請求上生成並在服務器上存儲pdf,如「/folder_name/uniqueID.pdf」。

將uniqueID作爲ajax響應返回(EXP:{「success:true」,「uniqueID:121212」})。然後在阿賈克斯成功部分寫

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; 
    } 
    } 

的download.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; 
     } 
}