0

EDITED:

Problem: I have a dynamically-generated html table on a page that I want the user to be able to click a button and download to their computer.

Setup: file called main.php which contains all processing, variables, arrays, etc. => feeds all data into webpages, one of which is table.php (this page dynamically echoes one of the arrays from main.php into an html table) => link in table.php to tblProcess.php with the headers that outputs the file for download.

code I have now in tblProcess.php file that works

header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
// Disable caching
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
header("Pragma: no-cache"); // HTTP 1.0
header("Expires: 0"); // Proxies

outputCSV(array(
  array("name 1", "age 1", "city 1"),
  array("name 2", "age 2", "city 2"),
  array("name 3", "age 3", "city 3")
));

function outputCSV($data) {
   $output = fopen("php://output", "w");
   foreach ($data as $row) {
       fputcsv($output, $row); // here you can change delimiter/enclosure
   }
   fclose($output);
}

link in table.php to tblProcess.php

<a id="dl" href="downloadProcess.php">Download</a>

with that dummy data array in the file, when you click the link all works as expected and it asks you to download filename.csv (with the dummy data in it) so I know the code is fine.

The big issues I'm having is how to get my html table from table.php into this file. Posting it via ajax and retrieving via $_POST hasn't worked at all no matter how I try, but I can't figure out how else to get this table into this file?

the way the table is echoed dynamically in table.php (from an array included from main.php)

  echo "<div id='assnTable'><table id='assign' border='1px'>";
  echo "<thead><th>uniqueID</th><th>Name</th><th>FundCode</th><th>Amount $</th><th>FundCode</th><th>Amount $</th><th>Totals:</th></thead>";
  $count = 1;
  echo "<tbody>";
  foreach($assn as $unID => $asnm) {
    oddClass($count);
    echo "<td>{$unID}</td>";
    echo "<td>";
    echo $stu[$unID]['USER_LAST_NAME'].", ".$stu[$unID]['USER_FIRST_NAME'];
    echo "</td>";
      foreach($asnm as $fund => $amt) {
        echo "<td>{$fund}</td>";
        echo "<td class='sumCell'>{$amt}</td>";
      }
    $count++;
    echo "</tr>";
  }
  echo "<tr class='totalRow'><td></td><td></td><td></td><td></td><td></td><td id='last'><b>Total:</b></td></tr>";
  echo "</tbody></table></div>";

In closing, my big question is, HOW do I get this html table into the processing file that downloads the file? I can clean and prepare it into an array or whatever I need, I just can't figure out how to get it into the other file. Trying to link an ajax post to the clicking of the download link disables the download part of it. Thanks!

essnbee
  • 1
  • 3
  • 1
    Because its an Ajax call not a regular form submit, the response is being lost due to you not doing anything with it in the success function. Just make it a normal form submit, and since its a file download it shouldn't change the page. – developerwjk Oct 22 '14 at 21:13
  • Also see http://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post – jeroen Oct 22 '14 at 21:16
  • @developerwjk how do I make my html table into a form that can be submitted that submits the proper parts? – essnbee Oct 23 '14 at 17:11

0 Answers0