php - POST in Javascript and redirect for file download


I've read many articles in this site or other sites (Redirect with POST to application/csv without form,, PHP and redirects, ... ) but without any valuable solutions. My problem is the following :

  1. in my site (html5, JQuery), there is a table. A feature of the site is to export the table as a csv file which will be available for download,
  2. This feature is implemented as follow :

    2.1 a javascript is called which extracts the data of the table,

    2.2 this JS redirect to a php service and pass as arguments the datas. The code is the following :

var url= jmcnet.request.getOrigin()+'/commons/php/dt_csv_export.php' ; location.href = url+"?action=generate&csv_type=export_task&csv_data=" +encodeURIComponent(csv);

2.3 The php script format the input (csv_data parameter), write a temporay file and returns the content of the temporary file. The code is the following :

$h = @fopen($csv_file_name, 'w');
      fputcsv($h, $csv_row, ',', '"');
      // export file content to JS
      header('Content-Encoding: UTF-8');
   header('Content-Type: text/ csv; charset =UTF-8');
   header('Content-Disposition: attachment; filename=export-table.csv');
   header(' Pragma: no-cache');
   echo "\xEF\xBB\xBF"; // UTF-8 BOM

2.4 The php file delete (unlink) the temporary file and exit,

My problem is that when the table is long, the URL called is not valid and the JS call to Php is down.

So, I imagine the 3 following solutions but no one is evident and all leads to other problems :

  1. S1 : dont do a GET but a POST in JS. So the size of the csv_data doesn't matter anymore. The problem is that I will have the content of the csv file in JS var after the call succeed and I don't know or find how to redirect to a page which content is in a JS var ? I guess I will lose all header information doing this.
  2. S2 : compress in JS the csv_data parameter and decompress it in Php. I just don't know how to do that and if it possible ....
  3. S3 : call the php with a POST. Modify the Php to return the URL of the temporary file, and do a redirect in JS to this temporay URL. The problems are that my Php must generate a file into a dir directly visible on the Internet, the file name must be unique and there is no way to simply delete the file after it has been read by browser (and I hate cron or what else).

I'm sure I'm not the first one to have this problem, so I need your help to see what is the best practice for this problem.




I think you may be over-complicating this just a bit. There is no need for all of the JS redirect stuff, you can just point your forms action attribute to your csv_export php code and use POST to send your data.

if needed, you can modify the max size of a post request by editing the post_max_size option in your php.ini. heres what mine looks like:

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
post_max_size = 8M

as for writing to a temporary file, php has built in I/O streams to handle that. for your purposes you'll probably want to use php://memory or php://temp (more info on those here:

so you can do something like this:


<!-- include jquery because you say you are using it -->
<script src="//"></script>

//just a dummy function to represent your js to extract csv data from a table
function extract_table_data(){
    var csv = "field1,field2,field3\n\
    return csv;

$( document ).ready(function() {
    //export link click handler
    $('#export_link').click(function() {
<a id='export_link'>Export CSV</a>
<form id='theform' method='post' action='dropcsv.php'>
    <input type='hidden' name='csv_data' id='csv_data'/>


//filename for our csv attachment
$export_filename = 'thefile.csv';
//grab csv data
$csv_data = $_POST['csv_data'];
//open file in memory
$f = fopen('php://memory', 'w'); //use php://temp if you want a tmp file instead
//load up csv file
fwrite($f, $csv_data);
// go back to the beginning of the file
fseek($f, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="'.$export_filename.'"');

of course don't forget to add your error checking and sanitize the input.

People are also looking for solutions to the problem: php - How to create While Loop Post in wordpress


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.