英文:
How to print login and signup details in a site using Admin button
问题
我使用HTML和CSS创建了一个登录表单,还创建了一个管理员按钮。当我点击管理员按钮时,它需要以表格格式打印已登录和注册用户的详细信息。
我正在学习JavaScript、HTML和CSS。因此,我创建了一个简单的登录表单,然后创建了一个管理员按钮。我决定为管理员按钮添加功能。
- 当我登录或注册时,我需要存储该数据。
- 当我点击管理员按钮时,我需要以表格格式打印存储的数据。
英文:
I created a login form using html and css, i had created a Admin button when i click on the admin button it needs to print the logged in and signup persons details in a table format.
Iam learning java script, html and css. So i created a simple login form and then i created a admin buton.I decided to give a functionality to admin buton.
- When i logged in or signup i need to store that data
2)I need to print the stored data when i clicked on admin button in tabular format.
答案1
得分: 1
步骤1: HTML表单:创建一个用于登录和注册的HTML表单,其中包含适当的输入字段,如用户名、密码和其他所需字段。以下是代码。
步骤2: 编写JavaScript代码来处理表单提交并存储登录/注册数据。
步骤3: JavaScript显示管理员表格:编写JavaScript代码,以表格形式显示存储的登录/注册数据,当单击管理员按钮时。
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言:lang-js -->
// 存储登录/注册数据
let userData = [];
// 处理登录表单提交
document.getElementById("loginForm").addEventListener("submit", function(event) {
event.preventDefault();
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
userData.push({
username,
password
});
this.reset();
});
// 处理注册表单提交
document.getElementById("signupForm").addEventListener("submit", function(event) {
event.preventDefault();
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
userData.push({
username,
password
});
this.reset();
});
document.getElementById("adminButton").addEventListener("click", function() {
let tableBody = document.querySelector("#adminTable tbody");
tableBody.innerHTML = ""; // 清除现有表格行
for (let user of userData) {
let row = document.createElement("tr");
let usernameCell = document.createElement("td");
let passwordCell = document.createElement("td");
usernameCell.textContent = user.username;
passwordCell.textContent = user.password;
row.appendChild(usernameCell);
row.appendChild(passwordCell);
tableBody.appendChild(row);
}
});
<!-- 语言:lang-html -->
<form id="loginForm">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<button type="submit">登录</button>
</form>
<form id="signupForm">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<button type="submit">注册</button>
</form>
<button id="adminButton">管理员</button>
<table id="adminTable">
<thead>
<tr>
<th>用户名</th>
<th>密码</th>
</tr>
</thead>
<tbody></tbody>
</table>
<!-- 结束代码片段 -->
Note: 请注意,您的HTML中有两个具有相同id的"username"和"password"输入字段,这不是有效的HTML。在实际应用中,您需要确保每个元素都有唯一的id。
英文:
Step 1: HTML Form: Create an HTML form for login and signup with appropriate input fields for username, password, and any other required fields, Here is code.
Step 2: Write JavaScript code to handle form submissions and store the login/signup data.
Step 3: JavaScript to Display Admin Table: Write JavaScript code to display the stored login/signup data in a tabular format when the admin button is clicked.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Store the login/signup data
let userData = [];
// Handle login form submission
document.getElementById("loginForm").addEventListener("submit", function(event) {
event.preventDefault();
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
userData.push({
username,
password
});
this.reset();
});
// Handle signup form submission
document.getElementById("signupForm").addEventListener("submit", function(event) {
event.preventDefault();
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
userData.push({
username,
password
});
this.reset();
});
document.getElementById("adminButton").addEventListener("click", function() {
let tableBody = document.querySelector("#adminTable tbody");
tableBody.innerHTML = ""; // Clear existing table rows
for (let user of userData) {
let row = document.createElement("tr");
let usernameCell = document.createElement("td");
let passwordCell = document.createElement("td");
usernameCell.textContent = user.username;
passwordCell.textContent = user.password;
row.appendChild(usernameCell);
row.appendChild(passwordCell);
tableBody.appendChild(row);
}
});
<!-- language: lang-html -->
<form id="loginForm">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<button type="submit">Login</button>
</form>
<form id="signupForm">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<button type="submit">Signup</button>
</form>
<button id="adminButton">Admin</button>
<table id="adminTable">
<thead>
<tr>
<th>Username</th>
<th>Password</th>
</tr>
</thead>
<tbody></tbody>
</table>
<!-- end snippet -->
答案2
得分: 0
我会在此之前说明,这可能不是最干净的解决方案,但希望对初学者来说是最容易理解的。
对于问题的第一部分,为表单上的注册按钮创建一个处理函数,该函数从表单字段中提取数据,创建一个包含这些数据的对象,并将其添加到用户数组中。
对于管理员功能,向您的HTML中添加一个空的表格元素。然后创建另一个处理函数,用于管理员按钮,该函数迭代用户数组并为数组中的每个用户对象创建一个作为您创建的表格元素的子元素的表格行。
如果您在理解这些内容时遇到困难,请发一些代码示例,我会给您一些进一步的指导
编辑:如果您遇到困难,Shanuka提供的代码示例应该会有所帮助。
英文:
I'll preface this by saying this won't be the cleanest solution, but hopefully the easiest to understand as a beginner.
For the first part of the problem, create a handler function for the signup button on the form which extracts the data from the form fields, creates an object from this data and adds it to an array of users.
For the admin functionality, add an empty table element to your HTML. Then create another handler, for the admin button, which iterates the array of users and creates, as a child of the table element you created, a table row for each of the user objects in the array.
If you have any trouble figuring these out post a code sample and I'll give you some further pointers
EDIT: If you do struggle, Shanuka's provided code samples should help
答案3
得分: 0
以下是您要翻译的内容:
Here's an example using an array to store the data:
Display Stored Data
Create a table in HTML where you can display the stored user details.
Show data
Trigger the Functions
英文:
> Here's an example using an array to store the data:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<script>
var users = []; // Array to store user details
function storeUserDetails(username, password) {
var user = {
username: username,
password: password
};
users.push(user);
}
function loginUser() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
storeUserDetails(username, password);
}
</script>
> Display Stored Data
Create a table in HTML where you can display the stored user details.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<table id="userTable" style="display: none;">
<thead>
<tr>
<th>Username</th>
<th>Password</th>
</tr>
</thead>
<tbody id="userTableBody">
<!-- User details will be added dynamically here -->
</tbody>
</table>
> Show data
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<script>
function showUserDetails() {
var table = document.getElementById("userTable");
var tableBody = document.getElementById("userTableBody");
// Clear table body
tableBody.innerHTML = "";
// Populate table with user details
for (var i = 0; i < users.length; i++) {
var user = users[i];
var row = tableBody.insertRow();
var usernameCell = row.insertCell();
var passwordCell = row.insertCell();
usernameCell.innerHTML = user.username;
passwordCell.innerHTML = user.password;
}
// Show the table
table.style.display = "table";
}
</script>
> Trigger the Functions
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<form onsubmit="loginUser()">
<!-- Login form fields -->
<input type="text" id="username" placeholder="Username">
<input type="password" id="password" placeholder="Password">
<button type="submit">Login</button>
</form>
<button onclick="showUserDetails()">Admin</button>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论