I have an assignment in which I need to use just html and javascript to store some records (let's just say a name and a phone number), that can be deleted/updated later. The catch is I can not use a database of any kind.
For simplicity sake, let's just say I just have a webpage that has 2 fields and 1 button: a name field, a phone number field, and a submit button. When the user presses submit, the information needs to be stored.
In my experience with Java, I would do something like this: get user input, save data to a .txt file. So I thought perhaps I can do something similar, however, in my search so far the responses I see make me believe that there is no way to create a .txt (or any type of) file using javasript/html.
So my question is just this: how can I save data using just html/javascript? Or is this even possible?
Thanks!
take a look: http://www.w3schools.com/HTML/html5_webstorage.asp
You can use web storage.
From W3Schools:
With web storage, web applications can store data locally within the user's browser.
Before HTML5, application data had to be stored in cookies, included in every server request. Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance.
Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.
Web storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.
You have a few options here:
The simplest way is HTML5 local storage. This is quite basic and allows you to set a name and value for each record. Take a look at https://developer.mozilla.org/en/docs/Web/API/Window/localStorage.
You can use either IndexedDB (https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) or Web SQL (http://www.tutorialspoint.com/html5/html5_web_sql.htm). Both of these are pretty similar in the sense that they both essentially create an offline database. However, some browsers support one and some support the other so it can be difficult to implement.
My personal favourite solution is YDN-DB (http://dev.yathit.com/ydn-db/index.html). It is a JavaScript library that uses all of the above methods to create a cross-browser compatible offline database. It can also sync with an online database as an added feature.
Hope this helps, feel free to leave a comment if you want a bit more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With