您好,我正在为其制作一个员工考勤管理系统,我有登录系统,其中包含两个界面,一个用于管理员,另一个用于员工。在员工界面中,我添加了“上班时间”和“超时时间”按钮。当员工点击时间按钮时,应该会出现一条警报,表明您已在 {当前时间} 成功计时,之后您一整天都无法再次计时,并且时间将成功保存在数据库中,反之亦然的时间逻辑这是我到目前为止所做的。
员工.php:
<button class="btn btn-success" onclick="timeIn()">Time In</button>
<button class="btn btn-danger" onclick="timeOut()">Time Out</button>
JS 函数:
function timeIn() {
// Check if the employee hasn't already clocked in
if (!localStorage.getItem('clockedIn')) {
$.ajax({
url: 'time_in.php',
type: 'POST',
data: {
employee_id: <?php echo $_SESSION['employee_id']; ?> // Pass the employee ID
},
success: function(response) {
if (response === 'success') {
localStorage.setItem('clockedIn', true);
alert('You have been successfully timed in.');
} else if (response === 'already_clocked_in') {
alert('You have already timed in.');
} else {
alert('Error occurred while timing in.');
}
},
error: function() {
alert('Error occurred while timing in.');
}
});
} else {
alert('You have already timed in.');
}
}
// JavaScript function to handle Time Out
function timeOut() {
// Check if the employee has clocked in
if (localStorage.getItem('clockedIn')) {
$.ajax({
url: 'time_out.php',
type: 'POST',
data: {
employee_id: <?php echo $_SESSION['employee_id']; ?> // Pass the employee ID
},
success: function(response) {
if (response === 'success') {
localStorage.removeItem('clockedIn');
alert('You timed out successfully.');
} else if (response === 'not_clocked_in') {
alert('You have not timed in yet.');
} else {
alert('Error occurred while timing out.');
}
},
error: function() {
alert('Error occurred while timing out.');
}
});
} else {
alert('You have not timed in yet.');
}
}
Time_in.php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
// Check if the user is authenticated as an employee
if (!isset($_SESSION['userType']) || $_SESSION['userType'] !== 'employee') {
header("Location: index.php");
exit;
}
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['employee_id'])) {
$employeeId = $_POST['employee_id'];
// Check if the employee has already clocked in
if (!isset($_SESSION['clockedIn'])) {
$currentDate = date('Y-m-d');
$currentTime = date('Y-m-d H:i:s');
$query = "INSERT INTO attendence_table (employee_id, time_in, attendance_date) VALUES (?, ?, ?)";
$stmt = mysqli_prepare($conn, $query);
if ($stmt) {
mysqli_stmt_bind_param($stmt, 'iss', $employeeId, $currentTime, $currentDate);
if (mysqli_stmt_execute($stmt)) {
$_SESSION['clockedIn'] = true; // Set clockedIn session variable
echo 'success';
} else {
echo 'error'; // Database error
}
mysqli_stmt_close($stmt);
} else {
echo 'error'; // Statement preparation error
}
} else {
echo 'already_clocked_in';
}
} else {
echo 'invalid_request';
}
mysqli_close($conn);
现在这段代码的问题是,当我单击按钮中的时间时,会弹出一个警报,指出“计时时发生错误”。但不知怎的,通过多个条目,时间被保存在数据库中,而错误是恒定的,而且当我在 time_in.php 响应中打开开发人员工具网络选项卡时,我添加了调试代码,它说已经_clocked_in,但我无法超时。有人可以帮助我在工作休息中腾出时间吗?我自己做。谢谢。
尽管这个解决方案很简单,但我已经在 ajax 中遇到过很多次了。你看...在你的代码中,开头之前有空格
要解决此问题,请先删除空格,然后再防止表单句柄吐出意外的空格。
this right here --> <?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
// Check if the user is authenticated as an employee
if (!isset($_SESSION['userType']) || $_SESSION['userType'] !== 'employee') {
header("Location: index.php");
exit;
}
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['employee_id'])) {
$employeeId = $_POST['employee_id'];
// Check if the employee has already clocked in
if (!isset($_SESSION['clockedIn'])) {
$currentDate = date('Y-m-d');
$currentTime = date('Y-m-d H:i:s');
$query = "INSERT INTO attendence_table (employee_id, time_in, attendance_date) VALUES (?, ?, ?)";
$stmt = mysqli_prepare($conn, $query);
if ($stmt) {
mysqli_stmt_bind_param($stmt, 'iss', $employeeId, $currentTime, $currentDate);
if (mysqli_stmt_execute($stmt)) {
$_SESSION['clockedIn'] = true; // Set clockedIn session variable
echo 'success';
} else {
echo 'error'; // Database error
}
mysqli_stmt_close($stmt);
} else {
echo 'error'; // Statement preparation error
}
} else {
echo 'already_clocked_in';
}
} else {
echo 'invalid_request';
}
mysqli_close($conn);