如何使用管理员按钮在网站上打印登录和注册详细信息

huangapple go评论81阅读模式
英文:

How to print login and signup details in a site using Admin button

问题

我使用HTML和CSS创建了一个登录表单,还创建了一个管理员按钮。当我点击管理员按钮时,它需要以表格格式打印已登录和注册用户的详细信息。

我正在学习JavaScript、HTML和CSS。因此,我创建了一个简单的登录表单,然后创建了一个管理员按钮。我决定为管理员按钮添加功能。

  1. 当我登录或注册时,我需要存储该数据。
  2. 当我点击管理员按钮时,我需要以表格格式打印存储的数据。
英文:

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.

  1. 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(&quot;loginForm&quot;).addEventListener(&quot;submit&quot;, function(event) {
event.preventDefault();
let username = document.getElementById(&quot;username&quot;).value;
let password = document.getElementById(&quot;password&quot;).value;
userData.push({
username,
password
});
this.reset();
});
// Handle signup form submission
document.getElementById(&quot;signupForm&quot;).addEventListener(&quot;submit&quot;, function(event) {
event.preventDefault();
let username = document.getElementById(&quot;username&quot;).value;
let password = document.getElementById(&quot;password&quot;).value;
userData.push({
username,
password
});
this.reset();
});
document.getElementById(&quot;adminButton&quot;).addEventListener(&quot;click&quot;, function() {
let tableBody = document.querySelector(&quot;#adminTable tbody&quot;);
tableBody.innerHTML = &quot;&quot;; // Clear existing table rows
for (let user of userData) {
let row = document.createElement(&quot;tr&quot;);
let usernameCell = document.createElement(&quot;td&quot;);
let passwordCell = document.createElement(&quot;td&quot;);
usernameCell.textContent = user.username;
passwordCell.textContent = user.password;
row.appendChild(usernameCell);
row.appendChild(passwordCell);
tableBody.appendChild(row);
}
});

<!-- language: lang-html -->

&lt;form id=&quot;loginForm&quot;&gt;
&lt;label for=&quot;username&quot;&gt;Username:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot; required&gt;
&lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt;
&lt;input type=&quot;password&quot; id=&quot;password&quot; name=&quot;password&quot; required&gt;
&lt;button type=&quot;submit&quot;&gt;Login&lt;/button&gt;
&lt;/form&gt;
&lt;form id=&quot;signupForm&quot;&gt;
&lt;label for=&quot;username&quot;&gt;Username:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot; required&gt;
&lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt;
&lt;input type=&quot;password&quot; id=&quot;password&quot; name=&quot;password&quot; required&gt;
&lt;button type=&quot;submit&quot;&gt;Signup&lt;/button&gt;
&lt;/form&gt;
&lt;button id=&quot;adminButton&quot;&gt;Admin&lt;/button&gt;
&lt;table id=&quot;adminTable&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Username&lt;/th&gt;
&lt;th&gt;Password&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;&lt;/tbody&gt;
&lt;/table&gt;

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

&lt;script&gt;
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(&quot;username&quot;).value;
var password = document.getElementById(&quot;password&quot;).value;
storeUserDetails(username, password);
}
&lt;/script&gt;

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

&lt;table id=&quot;userTable&quot; style=&quot;display: none;&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Username&lt;/th&gt;
&lt;th&gt;Password&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody id=&quot;userTableBody&quot;&gt;
&lt;!-- User details will be added dynamically here --&gt;
&lt;/tbody&gt;
&lt;/table&gt;

> Show data

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;script&gt;
function showUserDetails() {
var table = document.getElementById(&quot;userTable&quot;);
var tableBody = document.getElementById(&quot;userTableBody&quot;);
// Clear table body
tableBody.innerHTML = &quot;&quot;;
// Populate table with user details
for (var i = 0; i &lt; 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 = &quot;table&quot;;
}
&lt;/script&gt;

> Trigger the Functions

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;form onsubmit=&quot;loginUser()&quot;&gt;
&lt;!-- Login form fields --&gt;
&lt;input type=&quot;text&quot; id=&quot;username&quot; placeholder=&quot;Username&quot;&gt;
&lt;input type=&quot;password&quot; id=&quot;password&quot; placeholder=&quot;Password&quot;&gt;
&lt;button type=&quot;submit&quot;&gt;Login&lt;/button&gt;
&lt;/form&gt;
&lt;button onclick=&quot;showUserDetails()&quot;&gt;Admin&lt;/button&gt;

huangapple
  • 本文由 发表于 2023年6月19日 00:00:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定