Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect with POST to application/csv without form

I have a link/button on my page that should lead the user to a csv file. The csv file is generated dynamically using PHP, based on some POST variables:

<?php
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=e".date('ymdHis').".csv");
    ...
?>

How can I redirect to the script, at the same time passing POST variables, without using a form?

Answers I found that I didn't really like:

  • Create a hidden POST form with the desired variables and trigger a post using javascript
  • Use jQuery.post to load the page -- I don't yet see how I can use jQuery or AJAX in general to redirect the page
like image 880
marius Avatar asked Sep 01 '26 11:09

marius


1 Answers

You may use GET method - user won't see GET string (ugly URL), it won't be displayed in the new window. But if you really need POST (though i cannot even imagine why), you should create form with hidden fields - this is the only way to do your task (yes, i've tested many ways before saying this).

like image 84
shybovycha Avatar answered Sep 04 '26 18:09

shybovycha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!