2015-02-10 137 views
0

如果成功使用Ajax,我無法弄清楚如何讓可下載的csv文件可用。我將鏈接js ajax腳本以及我在Ajax函數中調用的php文件。感謝您的幫助。我將返回到我的Ajax函數的Success函數。我只是不知道如何將我的數據作爲可下載的csv文件返回。可下載的CSV文件通過Ajax

JS功能:

function popupClick2(){ 
    var popupObj2 = {}; 
    var c = '0'; 
    var p = '0'; 
    var i = '0'; 
    if (document.getElementById('checkboxC').checked){c = '1'} 
    if (document.getElementById('checkboxP').checked){p = '1'} 
    if (document.getElementById('checkboxI').checked){i = '1'} 
    popupObj2["checkboxC"] = c; 
    popupObj2["checkboxP"] = p; 
    popupObj2["checkboxI"] = i; 
    popupObj2["rangeD"] = $('#rangeD').val(); 
    popupObj2["year"] = $('#year').val(); 
    popupObj2["popupObj"] = '2'; 
    $.ajax({ 
     type: "POST", 
     dataType: "text", 
     url: "popupAjax.php", 
     data: popupObj2, 
     cache: false, 
     success: function(data) 
     { 
      alert("Success"); 
      //I would like to have the csv file downloadable here. 
     }, 
     error: function(jqXHR, textStatus, errorThrown) 
     { 
      console.log(jqXHR); 
      console.log(textStatus); 
      console.log(errorThrown); 
     } 
    }); 
    closePopup2(); 
} 

PHP(popupAjax.php)

<?php 
    $weekEnding = ''; 
    $PHSN = ''; 

    header('Content-Type: text/csv; charset=utf-8'); 
    header('Content-Disposition: attachment; filename=eWFO-Report.csv'); 

    $output = fopen('php://output', 'w'); 

    fputcsv($output, array('Week Ending', 'WN', 'Project Title', 'Project Contact', 
          'Org No', 'PHSN', 'No', 'Verified By', 'Date Verified', 
          'Comments', 'Notes')); 

    /*** connect to SQL DB ***/ 
    $dbe = get_db_connection('db'); 
    $dbe->connect(); 
    /*** connect or Oracle DB ***/ 
    $db = oci_connect('query','pw','server:1521/world'); 
    if (!$db){ 
     $e = oci_error(); 
     trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); 
    } 
    $query = "SELECT * FROM db.dbstuff WHERE (STATUS = 'ACTIVE' OR STATUS = 'CLOSED') AND NUMBER <> ' '"; 

    $runQuery = oci_parse($db, $query); 
    oci_execute($runQuery); 

    while($row = oci_fetch_array($runQuery, OCI_ASSOC+OCI_RETURN_NULLS)) 
    { 
     $WFON = $row['NUMBER']."-".$row['ANUMBER']; 

     $querySQLDB = "SELECT [Verified_By], [Comments], [Notes], [Date_Verified] 
       FROM dbo.Information 
       WHERE dbo.Information.Key_ID = '$WFON' 
       ORDER BY dbo.Information.ID DESC"; 
     $dbe->query($querySQLDB); 
     $sqlData = $dbe->fetch(); 

     $dateNoTime = str_replace("12:00:00:000AM"," ",$sqlData['Date_Verified']); 

     fputcsv($output, array($weekEnding, $WFON, $row['TITLE'], $row['NAME'], 
           $row['ORG'], $PHSNumber, $sqlData['Verified_By'], $dateNoTime, 
           $sqlData['Comments'], $sqlData['Notes'])); 

    } 
    echo $output; 
?> 
+2

你不應該爲此使用Ajax。只需使用一個普通的HTML表單與

' – developerwjk 2015-02-10 22:07:25

+0

....或iframe。 – symcbean 2015-02-10 22:21:00

回答

1

您可以添加一個形式,你popupClick2功能上最飛象:

function popupClick2(){ 

    ... 

    ($('<form/>', { 
     'id':  'tmpCsvForm', 
     'action': "popupAjax.php", 
     'method': 'post' 
    }).append($('<input />', { 
     'type': 'hidden', 
     'name': 'data', 
     'value': popupObj2 
    }))).appendTo('body'); 

    $('form#tmpCsvForm').submit().remove(); 
} 
+0

我從哪裏開始使用此代碼?就在Ajax之前,移除ajax? – vector 2015-02-10 23:14:20

+0

並感謝您的迴應! – vector 2015-02-10 23:14:54