A.Adams A.Adams - 4 years ago 91
Javascript Question

How can i store the registration details in local storage without overwriting it the next time?

So basically i have a registration form and everytime the user registers, i store their data in HTML local storage, but i want the data to look like this for example:
user| [{"username":"Maximillian","password":"whatever","address":""whatever".... and so on. So far i have this code but i am confused with why it doesn't work the way i want it to. And how do i make sure that when another person registers, their details does not overwrite the current register details already on local storage?

Answer Source

You could store an array of the users. For example:

var users = JSON.parse(localStorage.getItem('Users')) || [];
var userData = [{Username:document.getElementById("UserName").value},
{Email:document.getElementById("EmailAddress").value},
{Password:document.getElementById("PassWord").value},
{Address:document.getElementById("Address1").value},
{Phone:document.getElementById("PhoneNumber").value}];

users.push(userData);
localStorage.setItem('Users', JSON.stringify(users));

Then access the users as such:

var users = JSON.parse(localStorage.getItem('Users')) || [];
users.forEach(console.log);
users[0];
users[1];
[etc.]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download