允许用户将html表本地保存到csv,PHP和javascript

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!

doubian19900911
doubian19900911 如何将我的html表格变成可以提交正确部分的表单?
接近 6 年之前 回复
dongshan4549
dongshan4549 另见stackoverflow.com/questions/16086162/...
接近 6 年之前 回复
doupaoshu8334
doupaoshu8334 因为它的Ajax调用不是常规表单提交,所以由于您在成功函数中没有对其执行任何操作而导致响应丢失。只需将其作为普通表单提交,并且由于它是文件下载,因此不应更改页面。
接近 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐