Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi! I want to save my HTML form to a certain file in my computer. and whenever I click on submit, I want the file to get updated. and later I want to fetch the file data group wise. is this possible by using JS, AJAX, JSON only?

This is my local html file .

HTML
<aside id="sidebar">
    <div class="dark">
    <h3> Get a Quote</h3>
    <form class="quote" id="contactForm" method="POST" action="/contact">
    <div>
    <label>Name</label><br>
    <input type="text" name="Name" id="person">
    </div>
    <div>
    <label>Email</label><br>
    <input type="email" name="Email Address" id="email">
    </div>
    <div>
    <label>Message</label><br>
    <textarea placeholder="Message" name="query"></textarea>
    </div>
<input type="submit" value="Send">
</form>
</div>


Iam running localhost server by using node.js. Now, when I click on submit, I want the form input to be stored in the certain folder[for example C:\Users..] in JSON format like an array. For example, the file may look like

[
{ "person": "abc",
"email": "123@mail.com",
"message": "abcdefg",
},
{ "person": "efg",
"email": "456@mail.com",
"message": "bcdefg",
},
]
So when ever i am clicking on submit button the file should automatically get updated and save the next input as an array.
The second part is, fetching the store input value from recent to old when clicking on the button. so basically the second part would contain a basic HTML page with a button. and when I click on the button, it should show me the recent group of an array which is stored, for us from the above example the first click should show

{ "person": "efg", "email": "456@mail.com", "message": "bcdefg", },

and the next click should show

{ "person": "efg", "email": "456@mail.com", "message": "bcdefg", },
{ "person": "abc", "email": "123@mail.com", "message": "abcdefg", },

so basically in a decreasing order. and want to solve using AJAX. The first part is saving file input by use of JS, DOM, JSON. The second part is fetching file data AJAX. at present I do not want to use PHP, or ASP or any local browser storage, or online data base.

What I have tried:

I came across many forums, and I am getting confused, Suggest me regarding the codes and how to solve it. i know the second part, but I cannot able to do the first part.
Posted
Updated 7-Jul-18 22:27pm

1 solution

That's really not a good idea.
If you mean to store the file on the client, then you can't: Javascript / HTML doesn't have any direct access to the client file system for security reasons.

And if you mean on the server, then that's an even worse idea. While it's possible - and even simple, just convert it to JSON using one of the JSON packages out there and then use the file system IO to write it to disk - it's going to give you huge problems in production. Why? Because websites are inherently multiuser - and if you are writing the same file from multiple users you will get problems like "file in use" and worse: the last guy to write the file writes the only dtaa that anyone can read, everybody else's data gets thrown in the bin.

I would strongly suggest that you consider using a multiuser database system like SQL Server or MySQL instead of JSON - they are designed for such things where JSON is designed for data transport, not storage.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900