dongxia9519 2014-10-22 20:51
浏览 37

允许用户将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!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏