0

I have a php file, "php-1" which builds an html page.

It asks the user for some input The user clicks a button "getIDs" (by the way, there are several buttons with the same name)

That input is taken into a js / Ajax call, passing the input parameter to "php-2. (the passed parameter is actually assigned to the button that was clicked)

php-1

<script type="text/javascript">

jQuery(document).ready(function(){ 

  jQuery(document).find('button#getIDs').click(function(button){
    var parm1 = jQuery(this).attr('source_id');
    $.ajax({
      type: "POST",     
      url: "process-test.php?source_id="+parm1,
      cache: false,
      success: function(n) {    
        alert("complete");      
        } 
    }); 
 });
});
</script>

"php-2" takes the POST parameter (I suppose it's really a GET) and performs an API call to an external website, which returns 2 arrays. ex. [111, 222, 333, 444, 555] and [aaa, bbb, ccc]

(I know the arrays are being built, as I saved the array contents to a server file)

The arrays are "echoed" back to the Ajax code.

php-2:

echo $results123;
echo $resultsABC;

I would like this array saved off in 2 separate files on the user's machine, prompting the user for a directory and a .txt filename, each line containing a single array element:

file 1

111
222
333
...

file 2:

aaa
bbb
ccc

How do I achieve this?

Thanks in advance!

BTW, thanks to all the SO members. You have saved me days of effort already. Hope to be able to contribute to the community.

Cheers, Simon.

limeygent
  • 416
  • 6
  • 17
  • Have a look at this one and see if it works: http://stackoverflow.com/questions/40943/how-to-automatically-start-a-download-in-php – brandonscript Dec 13 '13 at 22:26

1 Answers1

0

You can write a file to it and encode the arrays in JSON, like this

  $location="path/to/file.txt";
  $file = fopen($location,"w");
  $content = JSON_encode($results123).JSON_encode($resultsABC);
  fwrite($file,$content);
  fclose($file);

Then, to download the file

header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($file));
header("Connection: close");
scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
  • this will save to the server first - correct? is there a way I can pass the arrays back to ajax/js and have them save the file locally instead? I would prefer not to keep the files on my server, but if that's the safest option, I can go that route. Thanks for the quick response. – limeygent Dec 13 '13 at 22:40
  • Yes, it will save it to the server – scrblnrd3 Dec 13 '13 at 22:41
  • (hit the return key too quickly - and then you replied almost immed!) is there a way I can pass the arrays back to ajax/js and have them save the file locally instead? I would prefer not to keep the files on my server, but if that's the safest option, I can go that route – limeygent Dec 13 '13 at 22:48
  • The update I provided should cover for that, it'll auto-download the file. You can `unlink($file)` if you need to delete it. Read more about that at http://de2.php.net/manual/en/function.unlink.php – scrblnrd3 Dec 14 '13 at 02:01
  • To put code in comments, enclose the code in backticks, like this `` – scrblnrd3 Dec 15 '13 at 17:40
  • I'm having challenges with formatting comments, so I'll describe what I did... in my first php ("php-1"), the js/jQuery calls php-2 which generates a server file and returns the filename back to php-1. If successful, the call is made to php-3 (has the code that you supplied above) which executes the download. Is this the intended solution - it seems like a lot of calls? – limeygent Dec 15 '13 at 18:02
  • I'm not quite sure what you're asking. Could you clarify, and maybe ask a new question, since this seems to be a new problem entirely? – scrblnrd3 Dec 15 '13 at 18:04
  • Thanks for the assistance @scrblnrd3. I have the code working. – limeygent Dec 19 '13 at 01:56