我有一个设计的注册表单,数据存储在本地存储中,我想在success.html中显示登录用户的数据。我试过使用下面的代码,但它不工作。请帮忙!如何显示用户使用javascript登录html页面的数据?
Login.html-
<!DOCTYPE html>
<html>
<title>Login</title>
<body>
<div class="col-md-6 col-md-offset-3">
<h2>Login</h2>
<form name="form" action="success.html" method="post">
<div class="form-group">
<label for="username">Username<span style="color: red;">*</span></label>
<input type="text" name="username" id="username" class="form-control" />
<span class="help-block" style="display:none">Username is required</span>
</div>
<div class="form-group" >
<label for="password">Password<span style="color: red;">*</span></label>
<input type="password" name="password" id="password" class="form-control" />
<span style="display:none">Password is required</span>
</div>
<div class="form-actions">
<button type="submit" onclick="validate()">Login</button>
<a href="register.html" class="btn btn-link">Register</a>
</div>
</form>
<div id="display"></div>
<script src="controller.js">
</script>
</div>
</body>
</html>
Controller.js--
localStorage.setItem("key_users", JSON.stringify(users));
var rusers = JSON.parse(localStorage.getItem('key_users'));
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
for(var p in rusers){
console.log(p+':'+rusers[p].username+'|'+rusers[p].email);
if(username==rusers[p].username && password==rusers[p].password){
alert("Logged in successfully");
function Redirect() {
window.location="success.html";
messageBox.innerHTML = "Welcome, " +rusers[p].firstname+ " here are your details: " + "</br>";
messageBox.innerHTML += "Name: " + rusers[p].firstname + "<br/>";
messageBox.innerHTML += "UserName: " + rusers[p].username + "<br/>";
messageBox.innerHTML += "EmailId: " + rusers[p].email + "<br/>";
}
}
}
success.html--
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<h2>Welcome User</h2>
<div id="display"></div>
<script src="controller.js">
</script>
</body>
</html>
什么是不正确的工作?你有任何错误? –
用户详细信息未显示在success.html中 – Poppins