用bootstrap做一个登陆注册页面的代码 1.注册成功会跳转到登陆 2.注册和登录可以切换 3.注册失败会有提示:用户名已经存在
时间: 2024-06-04 19:12:31 浏览: 151
<!DOCTYPE html>
<html>
<head>
<title>Login/Register Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container pt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs">
<li class="nav-item">
<a class="nav-link active" id="login-tab" data-toggle="tab" href="#login" role="tab" aria-controls="login" aria-selected="true">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" id="register-tab" data-toggle="tab" href="#register" role="tab" aria-controls="register" aria-selected="false">Register</a>
</li>
</ul>
</div>
<div class="card-body">
<div class="tab-content">
<div class="tab-pane fade show active" id="login" role="tabpanel" aria-labelledby="login-tab">
<form>
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" placeholder="Enter username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Enter password">
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
</div>
<div class="tab-pane fade" id="register" role="tabpanel" aria-labelledby="register-tab">
<form>
<div class="form-group">
<label for="new-username">Username</label>
<input type="text" class="form-control" id="new-username" placeholder="Enter new username">
</div>
<div class="form-group">
<label for="new-password">Password</label>
<input type="password" class="form-control" id="new-password" placeholder="Enter new password">
</div>
<div class="form-group">
<label for="confirm-password">Confirm Password</label>
<input type="password" class="form-control" id="confirm-password" placeholder="Confirm new password">
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$('#register-tab').click(function(){
$('#login-tab').removeClass('active');
$('#login').removeClass('show active');
$('#register-tab').addClass('active');
$('#register').addClass('show active');
});
$('#login-tab').click(function(){
$('#register-tab').removeClass('active');
$('#register').removeClass('show active');
$('#login-tab').addClass('active');
$('#login').addClass('show active');
});
$('form').submit(function(e){
e.preventDefault();
if($(this).attr('id') === 'register-form'){
var username = $('#new-username').val();
var password = $('#new-password').val();
var confirm_password = $('#confirm-password').val();
if(password !== confirm_password){
alert('Passwords do not match');
}else{
$.ajax({
url: 'register.php',
method: 'POST',
data: {
username: username,
password: password
},
success: function(response){
if(response === 'success'){
alert('Registration successful. Please login');
$('#register-tab').removeClass('active');
$('#register').removeClass('show active');
$('#login-tab').addClass('active');
$('#login').addClass('show active');
}else{
alert(response);
}
}
});
}
}else{
var username = $('#username').val();
var password = $('#password').val();
$.ajax({
url: 'login.php',
method: 'POST',
data: {
username: username,
password: password
},
success: function(response){
if(response === 'success'){
window.location.href = 'dashboard.php';
}else{
alert(response);
}
}
});
}
});
});
</script>
</body>
</html>
Note: The above code is just an example and will not work until you create the necessary PHP files for login and registration and reference them in the AJAX calls.
阅读全文