iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 14
1

今天星期日,猴子幹嘛去了?想知道的邦友可以回顧一下
Day05 PHP進化 判斷條件與時間函數
而我們今天瞧瞧註冊的表單,需要驗證哪些內容。

如果你是第一次看本系列的邦友,可以先點擊下方 Day01 文章,內含 30 秒的 Demo 短片跟原始碼下載。


玩轉 register.php

  • register Page Mods
<!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>


內容架構說明

  • Navigation 導覽列 
<?php require_once 'nav.php'; ?>

因為三個選單分頁都有導覽列,所以提取出來成一個 php 程式

表單

  • 表單位置
<form role="form">
</form>

什麼是 first name? last name?

  • 姓-Last Name、Surname、Family Name
  • 名-First Name、Forename、Given Name

RWD

  • 電腦版
  • 平板
  • 手機

動手改程式碼

  • 先開啟iTCafe網頁與用Sublime開啟register.php

    • 試著將所有The Perfet Cup改成你的咖啡店名:iTCafe
  • 立馬註冊一個測試帳號,出現錯誤

解決 Error

用 Sublime 開啟 adduser.php,root 後面加上密碼,更改完就可以在註冊一次囉
* 這是因為我們在Day09 SQL資料庫安全與帳號權限,有為root帳號加上密碼

//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost', 'root', '123456', 'perfectcup');

嘗試驗證系統 看會出現甚麼訊息

  1. 都不輸入資料,按 Submit
  2. 輸入名字後,嘗試輸入不正確的 EMAIL格式,按 Submit
  3. 輸入入名字後,嘗試正確的 EMAIL 格式,嘗試不同長度密碼(4-8碼),按Submit
  4. 各種天馬行空嘗試/images/emoticon/emoticon39.gif

總結

今天玩轉驗證註冊表單,不知道有沒有測出 BUG 呢?
明天我們來聊聊讓實現驗證功能的主角,你知道是誰嗎?我們明天揭曉答案


上一篇
Day13 翻玩 Blog.php -session & cokie
下一篇
Day15 jQuery 與 Ajax 驗證註冊表單
系列文
30天翻玩動態網站-會員需驗證才能看到部落格文章 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言