0

I am attempting to make a php script that returns a CSV file from a JSON array, but I am having trouble getting the info to the script. To make this CSV downloadable, I need to set the headers using php and create the CSV file on the fly (so I don't have to store it on my server), thus it seems I need to redirect the user's browser to my PHP script (aka no AJAX).

I want to post 3 variables. A user ID, a session string, and the stringified JSON array.

I could just use Jquery to create an input with the value of my JSON array, as such:

$('html-input-element').val(stringified-JSON-array);

But that does not seem ideal, as this JSON array could be thousands of lines long. Is there any way to do this purely in javascript? I would also rather use POST than GET because the session string is sensitive info. Thanks so much!

Community
  • 1
  • 1
user417669
  • 178
  • 3
  • 15
  • What's the problem with the input being thousands of lines long? The same thing happens if you send it via AJAX. – Barmar Jan 17 '14 at 23:33
  • Well it just seems a bit sloppy to add it to the html, when it is already a javascript variable. – user417669 Jan 17 '14 at 23:43
  • You'renot adding it to the HTML. You're just setting a hidden input value so you can include it in the form submission. – Barmar Jan 17 '14 at 23:46

1 Answers1

1

simply using jQuery and ajax: JFYI: input size limit is a server setting, it doesn't matter you're using ajax or form with input html:

<form id="downloaderForm" action="downloadCsv.php" method="post">
<intput type="hidden" name="json" id="jsonData" />
</form>

JQuery

 $("#jsonData").val(yourData);
 $("#downloaderForm").submit();

or JS

document.getElementById("jsonData").value = yourData;
document.getElementById("downloaderForm").submit();

PHP

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=json-cconverted-to-csv.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $csvData; // Your CSV Data
Shehabic
  • 6,787
  • 9
  • 52
  • 93
  • Ok, maybe I am misunderstanding, but I really don't want to store these files on my server, so I was hoping to create the file on the fly using the method described here: http://stackoverflow.com/questions/217424/create-a-csv-file-for-a-user-in-php/6493794#6493794 . The tricky thing is I need to make sure they should be able to create / access the file, using their session string and user id. What do you think? – user417669 Jan 17 '14 at 23:42
  • Then you should add the data to form, submit it via js, let the php script of your server convert it to CSV format, set the correct headers, and user will not be redirected, he'll be asked to download the file – Shehabic Jan 17 '14 at 23:44
  • So, to add the data to the form, do you mean add it to the html using the method I described? Or is there a way to do it in JS? – user417669 Jan 17 '14 at 23:45
  • What you wrote above is JS not HTML, and this is the only way to do it, and of course add it to a hidden element – Shehabic Jan 17 '14 at 23:46
  • @user417669 Think of hidden input elements as Javascript variables that get automatically sent to the server during form submission. – Barmar Jan 17 '14 at 23:47
  • Ok, I was hoping their was a way to do it a bit more manually, in javascript. leaving html input elements out of it. – user417669 Jan 17 '14 at 23:51