php - Download CSV file using "AJAX" -
I'm trying to accomplish a lot of simple work for my website, but I'm not sure how to actually do it I want that the user should see a table, then click on a button, at which point the user can save the contents of that table as a CSV file. This request can sometimes be very complex, so I have a progress page to alert the user.
In addition to creating a CSV file, I have many things I (use jQuery and PHP)
jQuery code running on click:
hmis_query_csv_export: function (query_name) {$ .uiLock ('& lt; p & gt; query being loaded. & Lt; / p & gt; & lt; img src = "/ images / loading.gif" /> ') $ .get ({url:' /php_scripts/utils/csv_export.php ', data: {query_name: query_name}, success: function (data) {$ .uiUnlock ();}})}}
related PHP:
header ("content-type: text / x-csv"); Header ("content-dispute: attachment; file name = search_results.csv"); // // Generate csv // echo $ csvOutput exit ();
What this does is send the text as a PHP file, but it does not generate a download. What am i doing wrong
If you are compelling a download, you download the link to the current page. Since the link will generate a download dialog, the current page (and its status) will be kept.
Basic approach:
$ ('a # query_name') .click (function () {$ ('# wait-animation') Show (); document .location.href = '/php_scripts/utils/csv_export.php?query_name='+query_name; $ (' # wait-animation ') Hide ();});
More complex:
$ ('a # query_name'). Click (Function () {MyTimestamp = new date}. GetTime (); // means the global hostile $ ('# wait-animation'). (); $ .get ('/ php_scripts / utils / csv_export .php ',' timestamp = '+ MyTimestamp +' & query_name = 'query_name, function () {document.location.href =' /php_scripts/refils/csv_export.php?timestamp='+MyTimestamp+'&query_name='+ Query_name; $ ('# wait-animation'). Hide ();});}); On PHP scripts
@header ("Last-modified:" @ GMDATE ("D, D MYH: i: s", $ _GET ['Timestamp']). "GMT"); @header ("content-type: text / x-csv"); // If the file is not requested through AJAX, download force (! Isset ($ _ SERVER ['HTTP_X_REQUESTED_WITH']) | strtolower ($ _ SERVER ['HTTP_X_REQUESTED_WITH']) = 'xmlhttprequest') {Header ("Content - Disposal: Attachment; File Name = search_results.csv"); } // // Generate csv // resize $ csvOutput exit ();
The URL for both requests should be identical to hide the browser, not a new download on document.location.href
, but the copy on it Cash to save me is not entirely sure about it, but looks very promising.
Comments
Post a Comment