js實現(xiàn)登陸與注冊功能
本文實例為大家分享了js實現(xiàn)登陸與注冊功能的具體代碼,供大家參考,具體內(nèi)容如下
1、首先在phpstudy文件中尋找到一個文件名叫 “www” 的文件 在里面創(chuàng)建html,js,php文件;
2、在Navicat 軟件中連接到phpstudy的MySQL;
3、在Navicat 軟件中尋找一個數(shù)據(jù)庫 并創(chuàng)建一個表格;
4、書寫html代碼(如下圖1)編寫簡單的注冊表單結(jié)構(gòu) 并通過js 給表單驗證;點擊注冊跳轉(zhuǎn)到php文件中;
5、php代碼(如下圖2) 首先獲取html代碼中表單的值 然后查找表單的數(shù)據(jù) 進行判斷 如果用戶存在就跳轉(zhuǎn)回到上個html頁面 用戶名設置成功后數(shù)據(jù)會自動保存到Navicat 軟件中的先前創(chuàng)建的表格中,保存之后跳轉(zhuǎn)到登陸頁面;
6、跳轉(zhuǎn)到登陸頁面 (如圖3);進入登陸頁面后可以輸入之前注冊的用戶名和密碼進行驗證 驗證過程是首先驗證用戶名是否存在然后驗證密碼是否正確 ; 用戶名不存在跳轉(zhuǎn)回去重新輸入 密碼不正確提用戶 重新輸入密碼;都正確之后跳轉(zhuǎn)到登陸的php文件中;
7、跳轉(zhuǎn)到登陸的php文件中(如圖4);獲取登陸名到數(shù)據(jù)庫中進行驗證 ;驗證哪里出問題就進行彈窗提示;如果驗證成功就把用戶名保存一份到瀏覽器中;
8、登陸成功后就可以跳轉(zhuǎn)到我們的首頁進行訪問
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="register.php" method="POST">
<table>
<caption>注冊</caption>
<tr>
<td>用戶名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>確認密碼:</td>
<td><input type="password" name="repass"></td>
</tr>
<tr>
<td>手機號碼:</td>
<td><input type="text" name="tel"></td>
</tr>
<tr>
<td>郵箱:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td><input type="submit" value="注冊"></td>
</tr>
</table>
</form>
</body>
<script>
var form = document.querySelector('form');
var username = document.querySelector('[name="username"]');
var password = document.querySelector('[name="password"]');
var repass = document.querySelector('[name="repass"]');
var tel = document.querySelector('[name="tel"]');
var email = document.querySelector('[name="email"]');
var btn1 = document.querySelector('[type="submit"]');
form.onsubmit = function(){
var reg = /^\w{4,12}$/;
if(!reg.test(username.value.trim())){
alert('請正確輸入用戶名');
return false;
}
var reg = /^\d{6,16}$/;
if(!reg.test(password.value.trim())){
alert('請正確輸入密碼');
return false;
}
if(password.value.trim() !== repass.value.trim()){
alert('兩次密碼輸入不正確');
return false;
}
var reg = /^1[3-9]\d{9}$/;
if(!reg.test(tel.value.trim())){
alert('請正確輸入手機號');
return false;
}
var reg = /^([1-9]\d{4,10}@qq|[a-zA-Z]\w{5,17}@(163|126))\.com$/;
if(!reg.test(email.value.trim())){
alert('請正確輸入郵箱');
return false;
}
}
</script>
</html>
<?php
// 修訂編碼格式
header("content-type:text/html;charset=utf8");
// 提取username的值
$username = $_POST['username'];
// 提取password的值
$password = $_POST['password'];
// 提取tel的值
$tel = $_POST['tel'];
// 提取email的值
$email = $_POST['email'];
// 連接數(shù)據(jù)庫
$con = mysqli_connect("localhost","root","root","test");
// 查找數(shù)據(jù)庫里面的用戶名
$res = mysqli_query($con,"select * from register where username='$username'");
// 查找一個數(shù)據(jù)庫的用戶名
$row = mysqli_fetch_assoc($res);
// 判斷用戶名是否存在
if($row){
echo ("<script>
alert('用戶名已被占用');
location.href='register.html';</script>");
}else{
// 給數(shù)據(jù)庫添加數(shù)據(jù)
$res = mysqli_query($con,"insert register(username,password,tel,email) values('$username','$password',$tel,'$email')");
// 判斷
if($res){
echo ("<script>
alert('注冊成功');
location.href='land.html';</script>");
}else{
echo ("<script>
alert('注冊失敗請重新注冊');
location.href='register.html';</script>");
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="land.php" method="POST">
<table>
<caption>登陸</caption>
<tr>
<td>用戶名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登陸"></td>
</tr>
</table>
</form>
</body>
<script>
var form = document.querySelector('form');
var username = document.querySelector('[name="username"]');
var password = document.querySelector('[name="password"]');
var btn1 = document.querySelector('[type="submit"]');
form.onsubmit = function(){
var reg = /^\w{4,12}$/;
if(!reg.test(username.value.trim())){
alert('請正確輸入用戶名');
return false;
}
var reg = /^\d{6,16}$/;
if(!reg.test(password.value.trim())){
alert('請正確輸入密碼');
return false;
}
}
</script>
</html>
<?php
header('content-type:text/html;charset=utf8');
$username = $_POST['username'];
$password = $_POST['password'];
$con = mysqli_connect("localhost","root","root","test");
$res = mysqli_query($con,"select * from register where username = '$username'");
$row = mysqli_fetch_assoc($res);
if($row){
if($row['password'] === $password){
setcookie('username',$username);
echo ("<script>
alert('登陸成功');
location.href='comment.html';</script>");
}else{
echo ("<script>
alert('密碼錯誤');
location.href='land.html';</script>");
}
}else{
echo ("<script>
alert('用戶名不存在');
location.href='land.html';</script>");
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Javascript 使用function定義構(gòu)造函數(shù)
Javascript并不像Java、C#等語言那樣支持真正的類。但是在js中可以定義偽類。做到這一點的工具就是構(gòu)造函數(shù)和原型對象。首先介紹js中的構(gòu)造函數(shù)。2010-02-02
javascript中負數(shù)算術(shù)右移、邏輯右移的奧秘探索
javascript中負數(shù)的算術(shù)右移和邏輯右移都十分的讓人迷惑,特別是邏輯右移,接下來的文章中將為大家詳細介紹下為什么右移之后,一個很小的負數(shù)也會得到一個無比巨大的數(shù)2013-10-10
JS正則匹配URL網(wǎng)址的方法(可匹配www,http開頭的一切網(wǎng)址)
這篇文章主要介紹了JS正則匹配URL網(wǎng)址的方法,可實現(xiàn)匹配www,http開頭的一切網(wǎng)址的功能,涉及JS正則匹配字符串、數(shù)字及特殊字符構(gòu)建URL的操作技巧,需要的朋友可以參考下2017-01-01

