/    Sign up×
Community /Pin to ProfileBookmark

URGENT! Need help in saving form data into localStorage

Hi. I need help in saving form data into local Storage for my school project. I’ve done a lot of research on google but I don’t know what went wrong with my code. Below are the test codes.

1.html

[CODE]
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Untitled Document</title>
<script type=”application/json”>
function storeEmail(){
var Emails = [];
if(localStorage.Emails) {
Emails = JSON.parse(localStorage.Emails);
}
Emails.push(getElementById(“email”).value);
localStorage.Emails = JSON.stringify(Emails);
}
</script>
</head>

<body>
<div style=”border:solid 1px;width:250px;”>
<form action=”2.html” method=”get” onSubmit=”storeEmail()”>
<p>Email</p>
<p>
<input name=”email” type=”email” required=”required” id=”email” placeholder=”Enter Email”/>
</p>
<p>
<input type=”submit” value=”submit”>
</p>
</form>
</div>
</body>
</html>
[/CODE]

2.html

[CODE]
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Untitled Document</title>
<script type=”application/json”>
function email_db(){
var Emails = [];
Emails = JSON.parse(localStorage.Emails);
var out = “”;
var i;
for(i = 0; i < localStorage.length; i++){
out += ‘<tr><td>’ + ++i + ‘</td><td>’ + Emails[i] + ‘</td></tr>’;
}
document.getElementById(“table”).innerHTML = out;
}
</script>
<style type=”text/css”>
table, tr, th, td {
border:solid 1px;
}
</style>
</head>

<body onLoad=”email_db()”>
<table id=”table”>
<tr><th>No.</th><th>Email</th></tr>
</table>
</body>
</html>
[/CODE]

I can’t add the data in 1.html into localStorage and I can’t read the data in 2.html from localStorage.
Please help.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @ReySalvatore spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...