Jump to content


Photo

Saving displayed table as CSV file.

html csv table php jquery

  • Please log in to reply
5 replies to this topic

#1 nottaclu

nottaclu

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 25 September 2013 - 03:22 PM

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!



#2 .josh

.josh

    .josh

  • Staff Alumni
  • 14,821 posts

Posted 25 September 2013 - 03:44 PM

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)

Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 nottaclu

nottaclu

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 25 September 2013 - 03:48 PM

Would the user then need to click file save in thier browser?



#4 .josh

.josh

    .josh

  • Staff Alumni
  • 14,821 posts

Posted 25 September 2013 - 04:05 PM

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

Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 .josh

.josh

    .josh

  • Staff Alumni
  • 14,821 posts

Posted 25 September 2013 - 04:15 PM

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' );


Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 nottaclu

nottaclu

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 25 September 2013 - 04:27 PM

I will give it a shot.  Thanks!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com