File: /var/www/w230/html/behavior/add_behavior.php
<?php
session_start();
$user_class = $_SESSION['user_class'];
if (!isset($_SESSION['user_id'])) {
header("Location: index.php");
exit();
}
include 'db.php';
// ดึงรูปผู้ใช้
$user_id = $_SESSION['user_id'];
$user_result = $conn->query("SELECT img FROM users WHERE id = '$user_id'");
$user_data = $user_result->fetch_assoc();
$user_img = !empty($user_data['img']) ? $user_data['img'] : 'default.png'; // กรณีไม่มีรูป
// ดึงรายชื่อนักเรียน
$students = $conn->query("SELECT id, fullname FROM students WHERE students.class='$user_class' ORDER BY fullname ASC");
// บันทึกข้อมูล
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$student_id = $_POST['student_id'];
$behavior = $_POST['behavior'] === 'อื่นๆ' ? $_POST['other_behavior'] : $_POST['behavior'];
$points = $_POST['points'];
$date = $_POST['date'];
$stmt = $conn->prepare("INSERT INTO behavior (student_id, behavior_detail, points, behavior_date) VALUES (?, ?, ?, ?)");
$stmt->bind_param("isis", $student_id, $behavior, $points, $date);
$stmt->execute();
$stmt->close();
header("Location: dashboard.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>บันทึกคะแนนพฤติกรรม</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
<link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Prompt&display=swap" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<style>
body {
font-family: 'Prompt', sans-serif;
background: linear-gradient(to right, #f0fff0, #ffffe0);
}
.container-fluid {
padding: 20px;
}
.footer-custom {
background: linear-gradient(to right, #a8e063, #fcd34d); /* เขียว -> เหลือง */
box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.2);
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="d-flex flex-column flex-md-row justify-content-between align-items-center mb-4">
<img src="uploads/<?= htmlspecialchars($user_img) ?>" class="rounded-circle me-2" width="120" height="125">
<h4 class="mb-2 mb-md-0">ยินดีต้อนรับคุณ <?= $_SESSION['user_name'] . " Class: " . $_SESSION['user_class']; ?></h4>
<a href="logout.php" class="btn btn-danger">ออกจากระบบ</a>
</div>
<div class="row g-3 mb-4">
<div class="col-12 col-sm-6 col-md-4">
<a href="add_behavior.php" class="btn btn-primary w-100"><i class="fa fa-blind"></i> บันทึกคะแนนพฤติกรรม</a>
</div>
<div class="col-12 col-sm-6 col-md-4">
<a href="dashboard.php" class="btn btn-info w-100"><i class="fa fa-line-chart"></i> รายงานคะแนน</a>
</div>
<div class="col-12 col-sm-6 col-md-4">
<a href="view_behavior.php" class="btn btn-success w-100"><i class="fa fa-user-o" aria-hidden="true"></i> รายงานคะแนนบุคคล</a>
</div>
</div>
<div class="card p-4 shadow-sm border-0 bg-white">
<h5 class="mb-3">ฟอร์มบันทึกพฤติกรรม</h5>
<form method="POST">
<div class="mb-3">
<label class="form-label">ชื่อนักเรียน</label>
<select name="student_id" class="form-select" required>
<option value="">-- เลือกนักเรียน --</option>
<?php while ($row = $students->fetch_assoc()): ?>
<option value="<?= $row['id'] ?>"><?= htmlspecialchars($row['fullname']) ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">พฤติกรรมที่กระทำ</label>
<select name="behavior" id="behavior-select" class="form-select" required>
<option value="">-- เลือกพฤติกรรม --</option>
<option value="มาสาย">มาสาย</option>
<option value="ทรงผม">ทรงผมผิดระเบียบ</option>
<option value="การแต่งกาย">การแต่งกายผิดระเบียบ/ไม่เรียบร้อย</option>
<option value="ทะเลาะวิวาท">ทะเลาะวิวาท</option>
<option value="พฤติกรรมเชิงชู้สาว">พฤติกรรมเชิงชู้สาว</option>
<option value="ออกนอกบริเวณโรงเรียนโดยไม่ได้รับอนุญาต">ออกนอกบริเวณโรงเรียนโดยไม่ได้รับอนุญาต</option>
<option value="พกอาวุธเข้ามาบริเวณโรงเรียนโดยไม่ได้รับอนุญาต">พกอาวุธเข้ามาบริเวณโรงเรียนโดยไม่ได้รับอนุญาต</option>
<option value="นำสิ่งเสพติดเข้ามาบริเวณโรงเรียน">นำสิ่งเสพติดเข้ามาบริเวณโรงเรียน</option>
<option value="อื่นๆ">อื่นๆ</option>
</select>
<div class="mt-2" id="other-behavior-group" style="display:none;">
<label class="form-label">ระบุพฤติกรรมอื่น ๆ</label>
<input type="text" name="other_behavior" id="other-behavior" class="form-control">
</div>
</div>
<div class="mb-3">
<label class="form-label">คะแนนที่หัก (ใส่ค่าลบ เช่น -5)</label>
<input type="number" name="points" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">วันที่</label>
<input type="date" name="date" class="form-control" required>
</div>
<div class="d-flex justify-content-between">
<a href="dashboard.php" class="btn btn-secondary">ย้อนกลับ</a>
<button type="submit" class="btn btn-success">✅ บันทึกข้อมูล</button>
</div>
</form>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const behaviorSelect = document.getElementById('behavior-select');
const otherGroup = document.getElementById('other-behavior-group');
const otherInput = document.getElementById('other-behavior');
behaviorSelect.addEventListener('change', function () {
if (this.value === 'อื่นๆ') {
otherGroup.style.display = 'block';
otherInput.setAttribute('required', 'required');
} else {
otherGroup.style.display = 'none';
otherInput.removeAttribute('required');
}
});
});
</script>
<?php include 'footer.php'; ?>
</body>
</html>