javascriptを使用して登録フォームを作成しました。そこから、ユーザーデータをローカルストレージ内のオブジェクトの配列に格納します。私はthis-localStorage.setItem( "key_users"、JSON.stringify(users))が好きでした。データは格納されません。 後は、実際に、あなたがのlocalStorageに空の配列を送信している私のcontroller.js-オブジェクトの配列からローカルストレージにデータを格納する方法は?
var users = [];
localStorage.setItem("key_users", JSON.stringify(users));
//var obj = JSON.parse(sessionStorage.users);
var idInput = makeCounter();
var firstnameInput = document.getElementById("firstname");
var lastnameInput = document.getElementById("lastname");
var emailInput = document.getElementById("email");
var usernameInput = document.getElementById("username");
var passwordInput = document.getElementById("password");
var messageBox = document.getElementById("display");
//document.getElementById("para").value=users[0].username;
//inputtext.innerHTML += users[0].firstname + "<br/>";
function userdetails(id, firstname, lastname, email, username, password){
this.id= id;
this.firstname= firstname;
this.lastname= lastname;
this.email= email;
this.username= username;
this.password=password;
this.FullName=this.firstname +' ' + this.lastname;
}
/* users.prototype.fullName = function() {
return this.firstname + " " + this.lastname;
} */
//Counter to increment id
function makeCounter() {
var arraylength=users.length;
return function(){
return i++;
}
/* var i = 0;
return function() {
return i++;
} */
}
//var id = makeCounter();
function insert()
{
var x = document.forms["myform"]["email"].value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
alert("Not a valid e-mail address");
return false;
}
var y = document.forms["myform"]["username"].value;
var z = document.forms["myform"]["password"].value;
var ulen= users.length;
for(i=0; i < ulen; i++)
{
if (y == users[i].username && z == users[i].password){
alert("User already exists");
return false;
}
}
var user=new userdetails(idInput(),firstnameInput.value, lastnameInput.value, emailInput.value, usernameInput.value, passwordInput.value);
users.push(user);
alert("Registered successfully");
// Clear the fields
firstnameInput.value = "";
lastnameInput.value = "";
emailInput.value = "";
usernameInput.value = "";
passwordInput.value = "";
}
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var ulen= users.length;
for(i=0; i < ulen; i++)
{
if (username == users[i].username && password == users[i].password){
alert("Logged in successfully");
}
messageBox.innerHTML = "Welcome, " +users[i].firstname+ " here are your details: " + "</br>";
messageBox.innerHTML += "Name: " + users[i].firstname + "<br/>";
messageBox.innerHTML += "UserName: " + users[i].username + "<br/>";
messageBox.innerHTML += "EmailId: " + users[i].email + "<br/>";
// messageBox.innerHTML += "FullName: " + userdetails.fullName() + "<br/>";
}
}
function editFirstname() {
var username = document.getElementById("username1").value;
var firstnameip = document.getElementById("firstname1").value;
var userlen= users.length;
for(i=0; i < userlen; i++)
{
if (username == users[i].username){
if(firstnameip == "" || firstnameip == null)
alert("name cant be blank");
else
users[i].firstname = firstnameip;
alert("firstname updated");
}else{
alert("Put your username to authenticate");
}
}
}
function editLastname() {
var username = document.getElementById("username1").value;
var lastnameip = document.getElementById("lastname1").value;
var userlen= users.length;
for(i=0; i < userlen; i++)
{
if (username == users[i].username){
if(lastnameip == "" || lastnameip == null)
alert("name cant be blank");
else
users[i].lastname = lastnameip;
alert("lastname updated");
}else{
alert("Put your username to authenticate");
}
}
}
function editEmail() {
var username = document.getElementById("username1").value;
var emailip = document.getElementById("email1").value;
var userlen= users.length;
for(i=0; i < userlen; i++)
{
if (username == users[i].username){
if(emailip == "" || emailip == null)
alert("name cant be blank");
else
users[i].email = emailip;
alert("email updated");
}else{
alert("Put your username to authenticate");
}
}
}
function editPassword() {
var username = document.getElementById("username1").value;
var passwordip = document.getElementById("password").value;
var userlen= users.length;
for(i=0; i < userlen; i++)
{
if (username == users[i].username){
if(passwordip == "" || passwordip == null)
alert("name cant be blank");
else
users[i].password = passwordip;
alert("password updated");
}else{
alert("Put your username to authenticate");
}
}
}
ありがとうございました..それは働いた。 – Poppins