HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux host 6.8.0-107-generic #107-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 13 19:51:50 UTC 2026 x86_64
User: w230 (1248)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/www/w230/html/election/public/index - Copy.php
<?php
include('../config/connect.php');
session_start();

if (!isset($_SESSION['student_code'])) {
    header("Location: form_login.php");
    exit;
}

$code = $_SESSION['student_code'];

// ตรวจสอบว่าโหวตแล้วหรือยัง
$check = mysqli_query($conn, "SELECT * FROM votes WHERE student_code = '$code'");
if (mysqli_num_rows($check) > 0) {
    echo "<script>alert('คุณได้ทำการลงคะแนนแล้ว');window.location='form_login.php';</script>";
    exit;
}

$candidates = mysqli_query($conn, "SELECT * FROM candidates ORDER BY number ASC");
?>
<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <title>ลงคะแนนเลือกตั้ง</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .circle-img {
            width: 160px;
            height: 160px;
            object-fit: cover;
            border-radius: 50%;
            border: 4px solid #0d6efd;
            box-shadow: 0 0 10px rgba(0,0,0,0.2);
            margin-bottom: 10px;
        }
        .card {
            align-items: center;
        }
    </style>
</head>
<body class="bg-light">
<div class="container mt-5">
    <h3 class="text-center mb-4">เลือกผู้สมัครที่คุณต้องการ</h3>
    <div class="row justify-content-center">
        <?php while ($row = mysqli_fetch_assoc($candidates)): ?>
        <div class="col-md-4 mb-4">
            <div class="card p-3">
                <img src="../assets/img/<?= $row['photo'] ?>" alt="<?= $row['name'] ?>" class="circle-img">
                <h5 class="mt-2"><?= $row['name'] ?> (หมายเลข <?= $row['number'] ?>)</h5>
                <form method="post" onsubmit="return confirmVote(this);">
                    <input type="hidden" name="candidate_id" value="<?= $row['id'] ?>">
                    <button type="submit" name="vote" class="btn btn-primary mt-2">โหวตให้หมายเลข <?= $row['number'] ?></button>
                </form>
            </div>
        </div>
        <?php endwhile; ?>
    </div>
</div>

<?php
// บันทึกการโหวต
if (isset($_POST['vote'])) {
    $candidate_id = $_POST['candidate_id'];
    $sql = "INSERT INTO votes (student_code, candidate_id) VALUES ('$code', '$candidate_id')";
    mysqli_query($conn, $sql);
    // หลังบันทึกคะแนนแล้วให้ redirect กลับผ่าน JS (ขอบคุณ)
    echo "<script>
        localStorage.setItem('voteSuccess', 'yes');
        window.location='form_login.php';
    </script>";
    exit;
}
?>

<script>
function confirmVote(form) {
    return confirm("คุณแน่ใจหรือไม่ที่จะโหวตให้ผู้สมัครคนนี้?");
}

window.onload = function() {
    if (localStorage.getItem('voteSuccess') === 'yes') {
        alert("ขอบคุณที่ลงคะแนน! ระบบจะนำคุณกลับไปยังหน้ากรอกรหัส");
        localStorage.removeItem('voteSuccess');
    }
};
</script>
</body>
</html>