Solved

Download to CSV file jquery

Posted on 2014-01-29
3
733 Views
Last Modified: 2014-02-04
Hello,
  I have data in an HTML table. I would like to read the data in the table and download it in a CSV format .

Basically, the table has a check box. name and address and download button. When the download button is press I would like to read the data in the selected row and force an http download of the file.  

Thanks for your help
0
Comment
Question by:SiemensSEN
  • 2
3 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39817778
You can't do this with javascript - you would need to use a server-side script, such as PHP.

When the download button is pressed, you would need to send your data, using Javascript / AJAX to a script on the server. That script would create the file, and then send back any info (filename /path etc.) back into Javascript, where you could then handle it however you like
0
 

Author Comment

by:SiemensSEN
ID: 39817791
Ok,

So, the procedure is:

JavaScript
-- Loop thru the table
-- construct a CSV string
-- use ajax and pass the CSV string to the server script

Server
-- Create a file with CSVString content
-- then force the HTTP download.

Thanks
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39817836
Yeah, pretty much, although you say you only want to send the selected row, so there should be no need to loop through the table.

You'll probably also find it easier to build the CSV string on the server-side. I know PHP has some useful CSV functions, such as fputcsv() - it takes a file pointer and an array, converts the array into a CSV string and writes it to a file - easy :)

In that case you'd just pass your individual values to the server using one of the jQuery ajax functions - ajax(), post() etc.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now