今天星期日,猴子幹嘛去了?想知道的邦友可以回顧一下
Day05 PHP進化 判斷條件與時間函數
而我們今天瞧瞧註冊的表單,需要驗證哪些內容。
如果你是第一次看本系列的邦友,可以先點擊下方 Day01 文章,內含 30 秒的 Demo 短片跟原始碼下載。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>The Perfect Cup - Register</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/business-casual.css" rel="stylesheet">
<!-- Fonts -->
<link href="http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Josefin+Slab:100,300,400,600,700,100italic,300italic,400italic,600italic,700italic" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Script -->
<script type="text/javascript">
$(document).ready(function () {
$("#register").click(function () {
fname = $("#fname").val();
lname = $("#lname").val();
email = $("#email").val();
password = $("#password").val();
$.ajax({
type: "POST",
url: "adduser.php",
data: "fname=" + fname + "&lname=" + lname + "&email=" + email + "&password=" + password,
success: function (html) {
if (html == 'true') {
$("#add_err2").html('<div class="alert alert-success"> \
<strong>Account</strong> processed. \ \
</div>');
window.location.href = "index.php";
} else if (html == 'false') {
$("#add_err2").html('<div class="alert alert-danger"> \
<strong>Email Address</strong> already in system. \ \
</div>');
} else if (html == 'fname') {
$("#add_err2").html('<div class="alert alert-danger"> \
<strong>First Name</strong> is required. \ \
</div>');
} else if (html == 'lname') {
$("#add_err2").html('<div class="alert alert-danger"> \
<strong>Last Name</strong> is required. \ \
</div>');
} else if (html == 'eshort') {
$("#add_err2").html('<div class="alert alert-danger"> \
<strong>Email Address</strong> is required. \ \
</div>');
} else if (html == 'eformat') {
$("#add_err2").html('<div class="alert alert-danger"> \
<strong>Email Address</strong> format is not valid. \ \
</div>');
} else if (html == 'pshort') {
$("#add_err2").html('<div class="alert alert-danger"> \
<strong>Password</strong> must be at least 4 characters . \ \
</div>');
} else {
$("#add_err2").html('<div class="alert alert-danger"> \
<strong>Error</strong> processing request. Please try again. \ \
</div>');
}
},
beforeSend: function () {
$("#add_err2").html("loading...");
}
});
return false;
});
});
</script>
</head>
<body>
<div class="brand">The Perfect Cup</div>
<!-- Navigation -->
<?php require_once 'nav.php'; ?>
<div class="container">
<div class="row">
<div class="box">
<div class="col-lg-12">
<hr>
<h2 class="intro-text text-center">Registration
<strong>form</strong>
</h2>
<div id="add_err2"></div>
<hr>
<form role="form">
<div class="row">
<div class="form-group col-lg-4">
<label>First Name</label>
<input type="text" id="fname" name="fname" maxlength="25" class="form-control">
</div>
<div class="form-group col-lg-4">
<label>Last Name</label>
<input type="text" id="lname" name="lname" maxlength="25" class="form-control">
</div>
<div class="form-group col-lg-4">
<label>Email Address</label>
<input type="email" id="email" name="email" maxlength="25" class="form-control">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label>Password</label>
<input type="password" id="password" name="password" maxlength="10" class="form-control">
</div>
<div class="form-group col-lg-12">
<button type="submit" id="register" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- /.container -->
<footer>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<p>Copyright © The Perfect Cup 2019</p>
</div>
</div>
</div>
</footer>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<?php require_once 'nav.php'; ?>
因為三個選單分頁都有導覽列,所以提取出來成一個 php 程式
<form role="form">
</form>
先開啟iTCafe網頁與用Sublime開啟register.php
立馬註冊一個測試帳號,出現錯誤
用 Sublime 開啟 adduser.php,root 後面加上密碼,更改完就可以在註冊一次囉
* 這是因為我們在Day09 SQL資料庫安全與帳號權限,有為root帳號加上密碼
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost', 'root', '123456', 'perfectcup');
Submit
Submit
Submit
今天玩轉驗證註冊表單,不知道有沒有測出 BUG 呢?
明天我們來聊聊讓實現驗證功能的主角,你知道是誰嗎?我們明天揭曉答案