Skip to content

Commit

Permalink
Sửa phần thông báo khi đăng ký hoặc đăng nhập
Browse files Browse the repository at this point in the history
  • Loading branch information
tutv committed May 10, 2015
1 parent 0e9fd67 commit 80fd67c
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 58 deletions.
25 changes: 14 additions & 11 deletions api/sign/signin.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
session_start();
include "../../config.php";
// lay password voi username nhap vao

function getPassword($username) {
global $conn;
$sql = "select pass from nguoidung where email = '$username'";
Expand Down Expand Up @@ -33,20 +33,23 @@ function getName($username) {
return "Bạn";
}

function echoJson($result) {
echo json_encode(array("result" => $result));
function echoJson($result, $notify = null) {
echo json_encode(array("result" => $result, "notify" => $notify));
}

if (isset($_POST['user']) and isset($_POST['pass'])) {
$pass = getPassword($_POST['user']);
if ($pass == false) {
echoJson(false);
$user = $_POST['user'];
$pass = $_POST['pass'];

if (isset($user) and isset($pass)) {
$passw = getPassword($user);
if ($passw == false) {
echoJson(false, "Không tồn tại người dùng này!");
} else {
if (md5( $_POST['pass']) == $pass) {
$_SESSION['user'] = getName($_POST['user']);
echoJson(true);
if (md5($pass) == $passw) {
$_SESSION['user'] = getName($user);
echoJson(true, "Đăng nhập thành công!");
} else {
echoJson(false);
echoJson(false, "Tài khoản hoặc mật khẩu không đúng!");
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions api/sign/signup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ function checkUser($username) {
return false;
}

function echoJson($result) {
echo json_encode(array("result" => $result));
function echoJson($result, $notify = null) {
echo json_encode(array("result" => $result, "notify" => $notify));
}

if (isset($_POST['user']) and isset($_POST['pass'])) {
$check = checkUser($_POST['user']);
$username = $_POST['user'];
$password = $_POST['pass'];
$user = $_POST['user'];
$pass = $_POST['pass'];

if (isset($user) and isset($pass)) {
$check = checkUser($user);

if($check == true) {
echoJson(false);
echoJson(false, "Tài khoản này đã có người dùng!");
} else {
$password = md5( $password );
$sql = "insert into nguoidung set email = '$username', pass = '$password'";
$passw = md5($pass);
$sql = "insert into nguoidung set email = '$user', pass = '$passw'";
mysqli_query( $conn, $sql );
echoJson(true);
echoJson(true, "Đăng ký thành công!");
}
}
else {
Expand Down
23 changes: 16 additions & 7 deletions includes/form-signin.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
<div class="modal fade" id="form-signin" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<form class="form-signin" role="form" method="post">
<label for="inputEmail" class="sr-only">Email</label>
<input type="email" name="username" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword" class="sr-only">Mật khẩu</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
<div class="checkbox">
<form class="form-signin" role="form" method="post" data-toggle="validator">
<div class="form-group">
<label for="inputEmail" class="sr-only">Email</label>
<input type="email" name="username" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="inputPassword" class="sr-only">Mật khẩu</label>
<input type="password" name="password" data-minlength="6" id="inputPassword" class="form-control" placeholder="Password" required>
<div class="help-block with-errors"></div>
</div>
<div class="checkbox form-group">
<label>
<input type="checkbox" value="remember-me"> Nhớ tôi
</label>
<div class="help-block with-errors"></div>
</div>
<button class="btn btn-lg btn-primary btn-block" type="button" id="btn-signin-submit">Đăng nhập</button>
<button class="btn btn-lg btn-primary btn-block" type="submit" id="btn-signin-submit">Đăng nhập</button>
</form>
</div>
</div>
Expand Down
24 changes: 18 additions & 6 deletions includes/form-signup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@
<div class="modal-dialog modal-sm">
<div class="modal-content">
<form class="form-signup" role="form" data-toggle="validator">
<label for="inputEmail" class="sr-only">Email</label>
<input type="email" id="inputEmail-up" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword-up" class="sr-only">Mật khẩu</label>
<input type="password" data-minlength="6" id="inputPassword-up" class="form-control" placeholder="Password" required>
<span class="help-block">Minimum of 6 characters</span>
<input type="password" class="form-control" id="inputPasswordConfirm" data-match="#inputPassword-up" data-match-error="Whoops, these don't match" placeholder="Confirm" required>
<div class="form-group">
<label for="inputEmail" class="sr-only">Email</label>
<input type="email" id="inputEmail-up" class="form-control" placeholder="Email address" required autofocus>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="inputPassword-up" class="sr-only">Mật khẩu</label>
<input type="password" data-minlength="6" id="inputPassword-up" class="form-control" placeholder="Password" required>
<span class="help-block">Tối thiểu 6 kí tự</span>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<input type="password" class="form-control" id="inputPasswordConfirm" data-match="#inputPassword-up" data-match-error="Whoops, these don't match" placeholder="Confirm" required>
<div class="help-block with-errors"></div>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" id="btn-signup-submit">Đăng ký</button>
</form>
</div>
Expand Down
2 changes: 1 addition & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ $(document).ready(function () {
dataType: "json",
success: function(data) {
if (data.status == true) {
signin();
signinX(data.user);
} else {
signout();
}
Expand Down
59 changes: 36 additions & 23 deletions js/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ function thongBao(text, type) {
});
}

//Đăng nhập
$("#btn-signin-submit").click(function () {
signinServer();
});

//Sign out khỏi hệ thống
$("#btn-signout").click(function() {
$.get("api/sign/signout.php");
Expand Down Expand Up @@ -59,6 +54,16 @@ function signin() {
}
});
}
//Thay đổi trang thái sang đã đăng nhập
function signinX(user) {
$("#hello-user").text(user);
var signinDOM = $("#signined");
signinDOM.show();
signinDOM.attr("data-target", "in");
$("#btn-signup").hide();
$("#btn-signin").hide();
}


//Get trạng thái đăng nhập
function getStatusSign() {
Expand Down Expand Up @@ -107,20 +112,6 @@ $("#btn-edit-profile").click(function() {
thongBao("<strong>Đang phát triển</strong><br>Chức năng đang được hoàn thiện", "information");
});

//Ấn Enter trực tiếp sau khi gõ xong không cần nhấn nút Đăng nhập
$('#inputPassword').keypress(function (e) {
if (e.which == 13) {
signinServer();
return false;
}
});
$('#inputEmail').keypress(function (e) {
if (e.which == 13) {
signinServer();
return false;
}
});

//Sign in vào server
function signinServer() {
var user = $("#inputEmail").val();
Expand All @@ -135,14 +126,21 @@ function signinServer() {
if (data.result == true) {
$("#form-signin").modal("hide");
signin();
thongBao("<strong>Đăng nhập</strong><br>Đăng nhập thành công!", "success");
thongBao("<strong>Đăng nhập</strong><br>" + data.notify, "success");
} else {
thongBao("<strong>Đăng nhập</strong><br>Đăng nhập thất bại!", "error");
thongBao("<strong>Đăng nhập</strong><br>" + data.notify, "error");
var temp = $("#form-signin");
var x = temp.attr("class");
temp.addClass("animated shake").one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
$(this).removeClass();
$(this).attr("class", x);
});
}
}
});
}

//Sign up vào server
function signupServer() {
var user = $("#inputEmail-up").val();
var pass = $("#inputPassword-up").val();
Expand All @@ -155,9 +153,15 @@ function signupServer() {
success : function(data) {
if (data.result == true) {
$("#form-signup").modal("hide");
thongBao("<strong>Đăng ký</strong><br>Bạn đã đăng ký thành công!", "success");
thongBao("<strong>Đăng ký</strong><br>" + data.notify, "success");
} else {
thongBao("<strong>Đăng ký</strong><br>Đăng ký thất bại! Vui lòng thử lại!", "error");
thongBao("<strong>Đăng ký</strong><br>" + data.notify, "error");
var temp = $("#form-signup");
var x = temp.attr("class");
temp.addClass("animated shake").one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
$(this).removeClass();
$(this).attr("class", x);
});
}
}
});
Expand All @@ -170,4 +174,13 @@ $('.form-signup').validator().on('submit', function (e) {
e.preventDefault();
signupServer();
}
});

$('.form-signin').validator().on('submit', function (e) {
if (e.isDefaultPrevented()) {
// handle the invalid form...
} else {
e.preventDefault();
signinServer();
}
});

0 comments on commit 80fd67c

Please sign in to comment.