Solved

Download to CSV file jquery

Posted on 2014-01-29
3
736 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)

772 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