Inside the click handler for that button:
window.location.href = "path/to/file/on/server.txt";
By the way, you don't actually have to write out a file in this case. You can use URL rewriting to make this a "virtual file", which runs your PHP code to emit the file data when accessed. You change the PHP to return the generated file data directly instead of writing it to a file.
EDIT: Virtual files in Apache:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /vfiles
RewriteRule ~vfiles/([a-z]+)\.txt vfile-handler.php?id=$1 [L,QSA]
</IfModule>
This says that http://my.server/vfiles/foo.txt gets transparently redirected inside the Apache server to run vfile-handler.php instead, passing it "id=foo" as the query string, which you deal with just like any other GET request with a query string. Your PHP script then sets the MIME type appropriately (text/plain
in this case, since we're claiming to be a .txt file) and builds the reply just as if we were building an HTML page on the fly.