用jquery mobile 和HTML5、PHP、JavaScript写一个登录注册页面
时间: 2023-12-27 10:04:51 浏览: 75
jQuery Mobile + PHP实现文件上传
好的,我可以给你一些基本思路和代码示例。
首先,你需要一个页面,包含登录和注册表单。可以使用jQuery Mobile框架来创建这些表单,示例代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login/Register Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Login/Register Page</h1>
</div>
<div data-role="content">
<form id="loginForm" method="post">
<h2>Login</h2>
<label for="username">Username:</label>
<input type="text" name="username" id="username" required>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required>
<button type="submit" data-theme="b">Login</button>
</form>
<form id="registerForm" method="post">
<h2>Register</h2>
<label for="newUsername">New Username:</label>
<input type="text" name="newUsername" id="newUsername" required>
<label for="newPassword">New Password:</label>
<input type="password" name="newPassword" id="newPassword" required>
<button type="submit" data-theme="b">Register</button>
</form>
</div>
</div>
</body>
</html>
```
接下来,你需要在PHP中编写处理登录和注册表单提交的代码。这些代码需要验证用户输入,并在数据库中查找用户信息。示例代码如下:
```php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Login form submitted
if (isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
// TODO: Validate user input here
// TODO: Check if user exists in database
// If user exists and password is correct, set session variable and redirect to home page
// If user doesn't exist or password is incorrect, show error message
}
// Register form submitted
if (isset($_POST['newUsername']) && isset($_POST['newPassword'])) {
$newUsername = $_POST['newUsername'];
$newPassword = $_POST['newPassword'];
// TODO: Validate user input here
// TODO: Check if username is already taken
// If username is available, insert new user into database and redirect to home page
// If username is taken, show error message
}
}
?>
```
最后,你需要使用JavaScript来处理表单提交,并显示错误消息或重定向到主页。示例代码如下:
```javascript
$(document).on('submit', '#loginForm', function(event) {
event.preventDefault();
var username = $('#username').val();
var password = $('#password').val();
// TODO: Validate user input here
// Submit form via AJAX
$.ajax({
url: 'login.php',
type: 'POST',
data: {
username: username,
password: password
},
success: function(response) {
if (response == 'success') {
// Redirect to home page
window.location.href = 'home.php';
} else {
// Show error message
alert('Invalid username or password');
}
}
});
});
$(document).on('submit', '#registerForm', function(event) {
event.preventDefault();
var newUsername = $('#newUsername').val();
var newPassword = $('#newPassword').val();
// TODO: Validate user input here
// Submit form via AJAX
$.ajax({
url: 'register.php',
type: 'POST',
data: {
newUsername: newUsername,
newPassword: newPassword
},
success: function(response) {
if (response == 'success') {
// Redirect to home page
window.location.href = 'home.php';
} else {
// Show error message
alert('Username already taken');
}
}
});
});
```
需要注意的是,这只是一个基本的示例,你需要根据自己的需求进行修改和扩展。
阅读全文