nottaclu Posted September 25, 2013 Share Posted September 25, 2013 I have a dynamic php page that has a series of select boxes at the top. When the user changes what is in the select boxes a div at the bottom of the page gets populated with a table via an ajax call to a php script. I am need of a button on that page that a user can click that will give them the option to save that displayed table as a .csv file. I have been looking all over for a solution that will work in both ie and ff, and am not coming up with anything. Any help would be great. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/282443-saving-displayed-table-as-csv-file/ Share on other sites More sharing options...
.josh Posted September 25, 2013 Share Posted September 25, 2013 javascript cannot save files. You can use javascript to grab the table data off the page and send it back to your server. You can do it via AJAX method if you want. Then you'd use php to just generate a file, you can use fputcsv (follow the example in the doc). If you do not want to save it to a file on your server, modify with this comment) Quote Link to comment https://forums.phpfreaks.com/topic/282443-saving-displayed-table-as-csv-file/#findComment-1451221 Share on other sites More sharing options...
nottaclu Posted September 25, 2013 Author Share Posted September 25, 2013 Would the user then need to click file save in thier browser? Quote Link to comment https://forums.phpfreaks.com/topic/282443-saving-displayed-table-as-csv-file/#findComment-1451222 Share on other sites More sharing options...
.josh Posted September 25, 2013 Share Posted September 25, 2013 If you want to save the file on your server, then if you want, you can return a link to the file to click. If you do not care about saving it to the server as a file, just the instructions in the comment link and it will return it back directly as a file to be output. It will open a dialog to the user to save or view it, same as directly clicking on any other yoursite.com/someFile.csv link Quote Link to comment https://forums.phpfreaks.com/topic/282443-saving-displayed-table-as-csv-file/#findComment-1451225 Share on other sites More sharing options...
.josh Posted September 25, 2013 Share Posted September 25, 2013 Make sure to output proper headers if you want to send it directly back to user: header( 'Content-Type: text/csv' ); header( 'Content-Disposition: attachment;filename=someFile.csv' ); Quote Link to comment https://forums.phpfreaks.com/topic/282443-saving-displayed-table-as-csv-file/#findComment-1451226 Share on other sites More sharing options...
nottaclu Posted September 25, 2013 Author Share Posted September 25, 2013 I will give it a shot. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/282443-saving-displayed-table-as-csv-file/#findComment-1451227 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.