Javascript and PHP export data to file
<p>I have a button. By clicking the button, I want to export some data shown on the webpage to a file for downloading. </p>

<p>I am doing this in this way: I have a <code>export.php</code>. I send the data as the parameter to PHP file (another parameter is filename), and PHP server create a file and write the data, and then send file. Code is like:</p>

<pre><code>$filename = $_GET['filename'] . '.csv';
$export = $_GET['export'];
$writer = fopen($filename, 'w') or die('cannot create');
fwrite($writer, $export . "\n");
fclose($writer);

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.basename($filename));
readfile($filename);
unlink($filename);

exit();
</code></pre>

<p>For cases that the data are short, it works fine. But if the data are long, since the data are passed as part of the URL, I will get "Request-URI Too Large" error.</p>

<p>Is there any alternative way to do that? Is it possible to directly write data using JavaScript?</p>
