Untitled

 avatar
unknown
plain_text
a month ago
1.4 kB
4
Indexable
<?php
session_start();
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../models/Student.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'];

    if ($action === 'register') {
        $result = Student::registerUser($_POST);
        echo json_encode($result);
    }

    if ($action === 'login') {
        $email = $_POST['email'];
        $password = $_POST['password'];

        $user = Student::loginUser($email, $password);
        if ($user) {
            $_SESSION['name'] = $user['first_name'];
            echo json_encode(['status' => 'success']);
        } else {
            echo json_encode(['status' => 'error', 'message' => 'Incorrect email or password.']);
        }
    }

    if ($action === 'addStudent') {
        $full_name = $_POST['full_name'];
        $email = $_POST['email'];
        $age = $_POST['age'];

        Student::addStudent($full_name, $email, $age);
        echo json_encode(['status' => 'success', 'message' => 'Student added successfully.']);
    }
}

if (isset($_GET['action']) && $_GET['action'] === 'logout') {
    session_destroy();
    header("Location: ../../login.php");
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'GET' && !isset($_GET['action'])) {
    $students = Student::getAllStudents();
    require_once __DIR__ . '/../views/student_list.php';
}
Editor is loading...
Leave a Comment