Getdachopa Getdachopa - 10 months ago 68
jQuery Question

Saving a data from html form to text file with jquery/javascript

I'm trying to create a comments tab for my website and insert an HTML form.

So far my form and comments tab is ready. I'd like to create a text document to store HTML form inputs.

I couldn't find a proper way to do that, like C++'s

. I found some code with PHP, but I don't want to use PHP either.

Any way to do this?

Leo Leo

You can use datauri and new download property of anchor elements (<a>) to achive it, without a server. Just randomly type something in the text box, click "export" and see what happens:

var container = document.querySelector('textarea');
var anchor = document.querySelector('a');

anchor.onclick = function() {
    anchor.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(container.value); = 'export.txt';
<p><a href="#">Export</a></p>

You can achive other types of file download, just change text/plain to the proper MIME type, and ensure that you encode the file content correctly. For example, <canvas> is a good approach for generating images.