From 1ce3f7f2a930893c7f7893bdd1fa3727e20528f2 Mon Sep 17 00:00:00 2001
From: freedomfeather <fadlurohmanakmal@rocketmail.com>
Date: Sun, 1 Oct 2017 11:37:21 +0700
Subject: [PATCH] Menambahkan ajax pada halaman sign up

---
 css/default_style.css |  7 +------
 login/login.html      |  2 +-
 login/login.php       |  2 +-
 login/sign_up.html    | 32 +++++++++++++++++++++++++++-----
 login/sign_up.php     | 33 +++++++++++++++++++++++++++++----
 5 files changed, 59 insertions(+), 17 deletions(-)

diff --git a/css/default_style.css b/css/default_style.css
index 85ddefe1..4305a148 100755
--- a/css/default_style.css
+++ b/css/default_style.css
@@ -15,15 +15,10 @@ body {
 }
 
 input{
-	width: 100%;
+	width: 90%;
 	margin: 1px auto 15px;
 }
 
-input[type="checkbox"]{
-	width: 0;
-	margin-right: 10px;
-}
-
 a:link, a:visited {
 	color: rgb(250,250,250);
 	text-decoration: none;
diff --git a/login/login.html b/login/login.html
index 12818487..821a4413 100755
--- a/login/login.html
+++ b/login/login.html
@@ -3,7 +3,7 @@
     <head>
         <title>Login to OneHundred</title>
         <link rel="stylesheet" type="text/css" href="../css/default_style.css">
-            <link rel="stylesheet" type="text/css" href="../css/login.css">
+        <link rel="stylesheet" type="text/css" href="../css/login.css">
     </head>
     <body>
         <div id="login_frame" class="frame">
diff --git a/login/login.php b/login/login.php
index 25c864af..6c740789 100755
--- a/login/login.php
+++ b/login/login.php
@@ -26,7 +26,7 @@
                 </script>";
                 header("Location: login.html");
             }
-            mysql_close($conn);
+            mysql_close();
         }
     }
 ?>
diff --git a/login/sign_up.html b/login/sign_up.html
index 33cafb7f..497590b9 100755
--- a/login/sign_up.html
+++ b/login/sign_up.html
@@ -2,7 +2,8 @@
 <html>
 <head>
 	<title>Sign Up</title>
-	<link rel="stylesheet" type="text/css" href="../css/main.css">
+    <link rel="stylesheet" type="text/css" href="../css/default_style.css">
+    <link rel="stylesheet" type="text/css" href="../css/login.css">
 </head>
 <body>
 	<div id="sign_up_frame" class="frame">
@@ -12,11 +13,11 @@
             <p id="error_signup" style="text-align: center;"></p>
 			<form name="sign_up" method="post" action="sign_up.php" onsubmit="return validate_password();">
 				Your name*
-				<input type="text" name="full_name"><br/>
+				<input type="text" name="full_name">
 				Username*
-				<input type="text" name="user_name"><br/>
+				<input type="text" name="username" onkeyup="checkAvailability(this.name,this.value)"><label id="username_verification"></label></br>
 				Email*
-				<input type="Email" name="user_email"><br/>
+				<input type="Email" name="user_email"onkeyup="checkAvailability(this.name,this.value)"><label id="email_verification"></label></br>
 				Password*
 				<input type="Password" name="user_password"><br/>
 				Confirm password*
@@ -25,7 +26,7 @@
 				<input type="text" name="user_phone"><br/>
 
                 <input type="button" value="Sign me up as driver" id="signAs_driver" style="width : 40%" onclick= "driver_signup();"><label id="driversign_status"><br></label>
-                <input type="text" name="is_driver" style="display:none">
+                    <input type="text" name="is_driver" value="No" style="display:none">
 				<input type="submit" value="REGISTER">
 			</form>
             <p><br>* Required field</p>
@@ -48,6 +49,27 @@
         document.getElementById("signAs_driver").style.display = "none";
         document.sign_up.is_driver.value = "Yes";
     }
+    function checkAvailability(name,value)
+    {
+        xhttp = new XMLHttpRequest();
+        if (value.length == 0) {
+            document.getElementById("username_verification").innerHTML = "";
+            document.getElementById("email_verification").innerHTML = "";
+            return;
+        }
+        xhttp.onreadystatechange = function() {
+            if (this.readyState == 4 && this.status == 200) {
+                if (name === "username") {
+                    document.getElementById("username_verification").innerHTML = this.responseText;
+                } else if (name === "user_email") {
+                    document.getElementById("email_verification").innerHTML = this.responseText;
+                }
+            }
+        };
+        var str = name+"="+value;
+        xhttp.open("GET", "sign_up.php?key="+str, true);
+        xhttp.send();
+    }
     </script>
 
 </body>
diff --git a/login/sign_up.php b/login/sign_up.php
index 6b972d10..f4dd99b4 100644
--- a/login/sign_up.php
+++ b/login/sign_up.php
@@ -1,7 +1,7 @@
 <?php
     if ($_SERVER["REQUEST_METHOD"] == "POST") {
         if (!empty($_POST['full_name']) &&
-            !empty($_POST['user_name']) &&
+            !empty($_POST['username']) &&
             !empty($_POST['user_email']) &&
             !empty($_POST['user_password']) &&
             !empty($_POST['confirm_password']) &&
@@ -14,7 +14,7 @@
             $password = $_POST['user_password'];
             $phone    = $_POST['user_phone'];
             
-            if(isset($_POST['is_driver']))
+            if($_POST['is_driver'] == "Yes")
             {
                 $status = 'driver';
             }
@@ -26,9 +26,13 @@
             $query = mysql_query("INSERT INTO user (name,email,phone,username,password,status,pict) VALUES ('$fullname', '$email', '$phone', '$username', '$password', '$status',DEFAULT)") or die(mysql_error());
             if($query)
             {
-                header("Location: ../order/order.html");
+                if ($status == "customer") {
+                    header("Location: ../order/order.html");
+                } else {
+                    header("Location: ../profile_page/profile.html");
+                }
             }
-            mysql_close($conn);
+            mysql_close();
         }
         else {
             include("sign_up.html");
@@ -37,5 +41,26 @@
             </script>";
             header("Location: sign_up.html");
         }
+    } else if ($_SERVER["REQUEST_METHOD"] == "GET") {
+        if (!empty($_GET['key'])) {
+            $key = stristr($_GET['key'],'=',true);
+            $value = substr(strstr($_GET['key'],'='),1);
+            
+            include '../database/dbconnect.php';
+            
+            if ($key == "username") {
+                $query = mysql_query("SELECT * FROM user WHERE username='".$value."'") or die(mysql_error());
+                $numrows=mysql_num_rows($query);
+            } else if ($key == "user_email") {
+                $query = mysql_query("SELECT * FROM user WHERE email='".$value."'") or die(mysql_error());
+                $numrows=mysql_num_rows($query);
+            }
+            if ($numrows != 0) {
+                echo " X";
+            } else {
+                echo " Ok";
+            }
+            mysql_close();
+        }
     }
 ?>
-- 
GitLab