1

I have a page user can dynamic add or edit html value like below, and I need to save it to database, I found a way- php read csv file and then update to database, but now my problem is

How to export html value(not table) to a csv file with jquery?

<div class="list">
    <div class="number">1</div>
    <div class="text">a</div>
    <div class="text">a</div>
</div>
<div class="list">
    <div class="number">1</div>
    <div class="text">a</div>
    <div class="text">a</div>
</div>
..
1
  • if you mean the number and letters in the divs, you need .text() Commented Jul 17, 2013 at 21:03

1 Answer 1

4

if you don't have comma-containing strings:

var csv = jQuery(".list").map(function(a,i){
  return $.trim($(this).text()).split(/\s*\n\s*/).join(",");
}).toArray().join("\r\n");

alert(csv);

you can use my downloader at http://danml.com/js/download.js to download the csv variable like:

 download(csv, "tabledata.csv", "text/csv");
Sign up to request clarification or add additional context in comments.

5 Comments

I use download.js but get console.log INVALID_CHARACTER_ERR: DOM Exception 5: An invalid or illegal character was specified, such as in an XML name. is it because I have chinese in html value ? how to fix it?
even if take off chinese character test it still get nterpreted as Document but transferred with MIME type text/csv and Unsafe JavaScript attempt to access frame with URL data:text/csv;base64,MywwLDIsMA0KMSwwLDEsMQ0KMCwwLDAsMA0KNiwwLDQsMA0KNCwwLDMsMA== from frame with URL about:blank. Domains, protocols and ports must match.
yes, chinese can break the atob() routine. i can likely fix that part, but which browser is giving you trouble?
Thanks so much! and the browser is safari 5, and so cool I test in firefox it works export a file to desktop. except chinese character can't be read in file. but after user click save I need to export the file in server folder then php open it. How to save the file to to server path??
ahh. i don't have any magic to make it save on the server, JS can only control the client machine. you will need to ask again with a php tag for info on that. I did update the script to support chinese, but Safari5 is an older browser that won't support these type of downloads. I've tested it in IE10, Chrome, and Firefox. Sorry about the confusion and good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.