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>
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>
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.