  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                คือผมทำระบบสมัครสมาชิกครับ ซึ่งผมต้องการให้มี error message สีเเดง เวลาที่ผู้ใช้กรอกข้อมูลผิด หรือเว้นว่างไว้ โดย code ที่ผมได้เขียนเอาไว้ตอนนี้สามารถเเจ้ง error ได้เเล้ว เเต่ผมให้มันเเจ้งไว้ในส่วนหัวข้อความ สมัครสมาชิก ซึ่งมันจะเเจ้งข้อผิดพลาดทั้งหมดตรงนี้ เเต่ที่ผมต้องการคือ ต้องการให้มีการเเจ้ง error message ตามช่องที่กรอกข้อมูล เช่น ไม่ได้กรอกช่อง Name ก็ให้เเจ้งตรงช่องชื่อ Name 
 
code หน้า Register 
Code (PHP) 
<?php
session_start();
require_once 'connect.php';
include 'Check_Register.php';
?>
<html>
    <head>
        <meta charset="utf8">
        <title>หน้าเเรก</title>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
       <!-- <script type="text/javascript" src="checkform.js"></script> -->
    </head>
    <body>     
        <div class="row">
            <div class="col-md-12"> <?php require_once 'headMenu_cus.php'; ?></div>
        </div>
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <h1>Siam Stanless</h1>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item ">
                        <a class="nav-link" href="Home.php">หน้าเเรก </a>
                    </li> 
                    <li class="nav-item ">
                        <a class="nav-link" href="Login.php">เข้าสู่ระบบ </a>
                    </li>    
                </ul>
            </div>
        </nav><br><br>
        <div class="container">
            <div class="row">
                <div class="col-md-3"></div>
                <div class="col-md-6">
                      	<?php include('errors.php'); ?>
  
                    <div class="alert alert-primary" role="alert" >
                        สมัครสมาชิก
                    </div>
                    <form action="Register.php" method="POST" onsubmit="return checkformregister(this);">
                        <table align="center" cellpadding="0" cellspacing="1" class="table table-hover">
                            <tr> 
                                
                                <td width="200" >คำนำหน้า <span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_perfix" type="text" class="box" id="cus_perfix"  value="<?php echo $cus_perfix; ?>" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="200" >Username <span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_username" type="text" class="box" id="cus_username"  value="<?php echo $cus_username; ?>" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="200" >ชื่อ <span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_name" type="text" class="box" id="cus_name"  value="<?php echo $cus_name; ?>" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="200">นามสกุล <span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_surname" type="text" class="box" id="cus_surname"  value="<?php echo $cus_surname; ?>" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="150">รหัสผ่าน <span class="label label-warning">*</span></td>
                                <td><input name="cus_password" type="password" class="box" id="cus_password" size="32" maxlength="32">
                                    <label class="label label-default" style="font-size:0.9em;">* อักษรผสมกับตัวเลข 8 ตัวอักษรขึ้นไป</label>
                                </td>
                            </tr>
                            <tr> 
                                <td width="150">ยืนยันรหัสผ่าน <span class="label label-warning">*</span></td>
                                <td> <input name="cus_confirm_passord" type="password" class="box" id="cus_confirm_passord" value="" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="150">อีเมล <span class="label label-warning">*</span></td>
                                <td> <input name="cus_email" type="email" class="box" id="cus_email" size="32"  value="<?php echo $cus_email; ?>" maxlength="32" placeholder="[email protected]"></td>
                            </tr> 
                            <tr> 
                                <td width="150">เบอร์โทรศัพท์ <span class="label label-warning">*</span></td>
                                <td> <input name="cus_number" type="tel" class="box" id="cus_number"  value="<?php echo $cus_number; ?>" size="32" maxlength="32"></td>
                            </tr>
                        </table>
                        <p align="center"> 
                            <input type="submit" value="สมัครสมาชิก" name="reg_user" class="btn btn-primary">
                            <input type="button" value="ยกเลิก" onclick="window.location.href = 'Home.php'" class="btn btn-primary">
                        </p>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
 
 
code หน้า check_register 
Code (PHP) 
<meta charset="utf8">
<?php
error_reporting(error_reporting() & ~E_NOTICE);
session_start();
require_once 'connect.php';
$cus_perfix = "";
$cus_username = "";
$cus_name = "";
$cus_surname = "";
$cus_email = "";
$cus_number = "";
$cus_password = $_POST['cus_password'];
$cus_hash = md5($cus_password); // นำตัวเเปร cus_password มาเข้ารหัส เเบบ md5 
$errors = array();
if (isset($_POST['reg_user'])) {
    $cus_perfix = mysqli_real_escape_string($con, $_POST['cus_perfix']);
    $cus_username = mysqli_real_escape_string($con, $_POST['cus_username']);
    $cus_name = mysqli_real_escape_string($con, $_POST['cus_name']);
    $cus_surname = mysqli_real_escape_string($con, $_POST['cus_surname']);
    $cus_email = mysqli_real_escape_string($con, $_POST['cus_email']);
    $cus_number = mysqli_real_escape_string($con, $_POST['cus_number']);
    if (empty($cus_perfix)) {
        array_push($errors, "กรุณากรอกคำนำหน้า");
    }
    if (empty($cus_username)) {
        array_push($errors, "กรุณากรอก username");
    }
    if (empty($cus_name)) {
        array_push($errors, "กรุณากรอกชื่อ");
    }
    if (empty($cus_surname)) {
        array_push($errors, "กรุณากรอกนามสกุล");
    }
    if (empty($cus_email)) {
        array_push($errors, "กรุณากรอก Email ");
    }
    if (empty($cus_number)) {
        array_push($errors, "กรุณากรอกเบอร์โทรศัพท์");
    }
    $sql = "SELECT * FROM customer WHERE cus_username = '$cus_username' ";
    $result = mysqli_query($con, $sql); // นำมา query ข้อมูลเพื่อให้ mysql server ประมวลผล
    $sql2 = "SELECT * FROM customer WHERE cus_email = '$cus_email'  "; 
    $result2 = mysqli_query($con, $sql2);
    if (mysqli_num_rows($result) > 0) {
        array_push($errors, "Username already exists");
    } else if (mysqli_num_rows($result2) > 0) {
        array_push($errors, "Email already exists");
    } else if (checkPassword($cus_password) == false) { // เป็นการใช้ function checkPassword เพื่อตรวจสอบว่ามีค่าเป็นเท็จหรือไม่ 
        array_push($errors, "cus_password already exists");
    } else if (checkEmail($cus_email) == false) {
        array_push($errors, "cus_email already exists");
    } else if (checkUsername($cus_username) == false && $cus_username != '') {
        array_push($errors, "cus_username already exists");
    } else if (checkNumber($cus_number) == false && $cus_number != '') { // เป็นการใช้ function checkNumer เพื่อตรวจสอบว่ามีค่าเป็นเท็จหรือไม่ เเละตรวจสอบว่า ไม่ได้เป็นค่าว่าง 
        array_push($errors, "cus_number already exists");
    
    } else {
        $sql = "INSERT INTO customer (cus_perfix, cus_username, cus_name, cus_surname, cus_password, cus_email, cus_number) "
                . "VALUES ('$cus_perfix','$cus_username', '$cus_name', '$cus_surname', '$cus_hash', '$cus_email', '$cus_number'')";
        $result1 = mysqli_query($con, $sql);
        if ($result1) {
            echo "<script>alert('บันทึกข้อมูลเรียบร้อยแล้ว');window.location='Login.php';</script>";
        } else {
            echo "<script>alert('บันทึกข้อมูลไม่สำเร็จ');window.location='Register.php';</script>";
        }
    }
}
?>
 
 
code หน้า error 
Code (PHP) 
<?php  if (count($errors) > 0) { ?>
  <div class="error">
  	<?php foreach ($errors as $error) { ?>
  	  <p><?php echo $error ?></p>
        <?php } ?>
  </div>
<?php  } ?>
 
 
  Tag : PHP, HTML5, JavaScript, Ajax, jQuery               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2019-02-19 01:03:03	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2019-02-19 01:01:26 | 
                      By :
                          staff39 | 
                      View :
                          615 | 
                      Reply :
                          1 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  |