 |
|
ผมไม่แน่ใจว่ามันส่งค่าจากไฟล์ check-login ไปมั้ย ลองล็อกอินมันขึ้น Problem with sql query งงว่ามันไม่เข้าเงื่อนไขตรงไหน รบกวนช่วยดูทีครับ
check-login.php
Code (PHP)
<?php
include 'connect.php'; // include the library for database connection
if(isset($_POST['action']) && $_POST['action'] == 'login'){ // Check the action `login`
$username = trim($_POST['username']); // Get the username
$password = trim($_POST['password']); // Get the password and decrypt it
$sql = $db->prepare('SELECT *
FROM tbl_user
WHERE uLogin = :username AND uPassword = :password' );
$sql->bindParam(':username', $username);
$sql->bindParam(':password', $password);
$sql->execute();
$rowCount = $sql->rowCount();
$results = $sql->fetch(PDO::FETCH_ASSOC);
if($rowCount > 0){
$_SESSION['user'] = $results['uLogin'];
$_SESSION['pass'] = $results['uPassword'];
$_SESSION['fullname'] = $results['uFullName'];
echo 1;
}
else { // If no users exist with posted credentials print 0 like below.
echo 0;
}
}
?>
connect.php
Code (PHP)
<?php
session_start();
date_default_timezone_set('Asia/Bangkok');
error_reporting (E_ALL ^ E_NOTICE);
try {
$db = new PDO('mysql:host=localhost;dbname=statis;charset=utf8', 'root', 'root');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Database connected";
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
Login.php
Code (PHP)
<script type="text/javascript">
$(document).ready(function(){
$('#username').focus(); // Focus to the username field on body loads
$('#login').click(function(){ // Create `click` event function for login
var username = $('#username'); // Get the username field
var password = $('#password'); // Get the password field
var login_result = $('.login_result'); // Get the login result div
login_result.html('<img src=img/ajax-load.gif /> loading..'); // Set the pre-loader can be an animation
if(username.val() == ''){ // Check the username values is empty or not
username.focus(); // focus to the filed
login_result.html('<span class="error red-text">กรุณากรอกชื่อผู้ใช้</span>');
return false;
}
if(password.val() == ''){ // Check the password values is empty or not
password.focus();
login_result.html('<span class="error red-text">กรุณากรอกรหัสผ่าน</span>');
return false;
}
if(username.val() != '' && password.val() != ''){ // Check the username and password values is not empty and make the ajax request
var UrlToPass = 'action=login&username='+username.val()+'&password='+password.val();
$.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
type : 'POST',
data : UrlToPass,
url : 'check-login.php',
success: function(responseText){ // Get the result and asign to each cases
if(responseText == 0){
login_result.html('<span class="error red-text">ชื่อผู้ใช้หรือรหัสผ่านผิด !</span>');
}
else if(responseText == 1){
window.location = 'index.php';
}
else{
alert('Problem with sql query');
}
}
});
}
return false;
});
});
</script>
Tag : PHP, MySQL, JavaScript, Ajax, jQuery
|
ประวัติการแก้ไข 2015-05-28 10:20:56 2015-05-28 14:18:39
|
 |
 |
 |
 |
Date :
2015-05-28 10:02:30 |
By :
littlebeer |
View :
1618 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |