前端html+css+js+数据库注册+登录案例
时间: 2023-07-25 18:31:22 浏览: 166
前端—css、js—登录注册一体的网站模版
好的,这是一个简单的前端注册登录案例,使用了HTML、CSS、JavaScript和MySQL数据库。
首先,我们需要创建一个HTML页面,包含注册和登录表单。以下是示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册登录案例</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>注册</h1>
<form>
<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" id="register-btn">注册</button>
</form>
</div>
<div class="container">
<h1>登录</h1>
<form>
<label for="login-username">用户名</label>
<input type="text" id="login-username" name="login-username" required>
<label for="login-password">密码</label>
<input type="password" id="login-password" name="login-password" required>
<button type="submit" id="login-btn">登录</button>
</form>
</div>
<script src="script.js"></script>
</body>
</html>
```
接下来,我们需要创建一个CSS文件来样式化我们的页面。以下是示例代码:
```css
.container {
display: inline-block;
margin: 20px;
padding: 20px;
border: 1px solid black;
border-radius: 5px;
}
form {
display: flex;
flex-direction: column;
}
label {
margin-top: 10px;
}
input {
margin-bottom: 10px;
padding: 5px;
border-radius: 3px;
border: 1px solid grey;
}
button {
margin-top: 10px;
padding: 5px;
border-radius: 3px;
border: none;
background-color: blue;
color: white;
}
```
然后,我们需要创建一个JavaScript文件来处理表单提交和与数据库的交互。以下是示例代码:
```javascript
const registerForm = document.querySelector('#register-form');
const loginForm = document.querySelector('#login-form');
registerForm.addEventListener('submit', async (e) => {
e.preventDefault();
const username = registerForm.elements.username.value;
const password = registerForm.elements.password.value;
const response = await fetch('/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
const data = await response.json();
if (data.success) {
alert('注册成功!');
} else {
alert('注册失败,请重试!');
}
});
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const username = loginForm.elements['login-username'].value;
const password = loginForm.elements['login-password'].value;
const response = await fetch('/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
const data = await response.json();
if (data.success) {
alert('登录成功!');
} else {
alert('登录失败,请重试!');
}
});
```
最后,我们需要创建一个后端服务器,处理注册和登录请求,并将用户数据存储在MySQL数据库中。以下是示例代码:
```javascript
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const app = express();
const port = 3000;
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'users'
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to database: ' + err.stack);
return;
}
console.log('Connected to database with ID ' + connection.threadId);
});
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.post('/register', (req, res) => {
const { username, password } = req.body;
const query = `INSERT INTO users (username, password) VALUES ('${username}', '${password}')`;
connection.query(query, (err, results) => {
if (err) {
console.error('Error registering user: ' + err.stack);
res.json({ success: false });
return;
}
res.json({ success: true });
});
});
app.post('/login', (req, res) => {
const { username, password } = req.body;
const query = `SELECT * FROM users WHERE username='${username}' AND password='${password}'`;
connection.query(query, (err, results) => {
if (err) {
console.error('Error logging in user: ' + err.stack);
res.json({ success: false });
return;
}
if (results.length > 0) {
res.json({ success: true });
} else {
res.json({ success: false });
}
});
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
```
这样,我们就完成了一个简单的前端注册登录案例,使用了HTML、CSS、JavaScript和MySQL数据库。当用户在注册表单中输入用户名和密码时,通过JavaScript将数据发送到后端服务器,服务器将数据存储在数据库中。当用户在登录表单中输入用户名和密码时,通过JavaScript将数据发送到后端服务器,服务器将查询数据库以验证用户身份并返回结果。
阅读全文