1

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 fstream. I found some code with PHP, but I don't want to use PHP either.

Any way to do this?

1
  • 1
    If you want to store data from an HTML form on a server you have to use some backend written in whatever language you would like to use. Most popular is surely PHP or maybe Python. If you want to use JavaScript you have to write an plugin for your Apache (if there isn't any), but I would be really concerned about the security than. Commented Dec 10, 2014 at 10:05

1 Answer 1

11

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);
    anchor.download = 'export.txt';
};
<textarea></textarea>
<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.

Sign up to request clarification or add additional context in comments.

4 Comments

That works, thanks. But for example, 10 people commented on my web site, how am I suppose to see all of the comments with one single .txt? This code exports current .txt when you press export, right?
@Getdachopa In such case you need to grab all comments, format and concatenate, then you get the text content, and generate the file down. However, for this purpose I suggest you output HTML over text, because HTML may have better readability with the help of CSS.
how can i use this without anchor tag, by a button
@Prem The href and download attributes only apply to <a> elements, so you can't do this without it. However, you could make the <a> element invisible, and call its click() method when your <button> is clicked - button.onclick = function() { anchor.click(); };

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.