TimeoutAndRedirect is not defined at onload()

I’m trying to login via AJAX but it looks like I can’t do this… Basically, what I’m trying to do is that, based on the user type, after login, I should be redirected to a certain page. The call looks like this: <body onload="TimeoutAndRedirect('student')">
Sometimes I can login, sometimes I can’t, and I don’t understand why… Here’s my timeoutandredirect function that I’ve initially used cookies and XHR requests, but it’s too slow:

function TimeoutAndRedirect(Type) {
     $.ajax
    ({
  type: "GET",
  url: "../php/redirect.php",
  success: function(respond)
  {

            if (respond == "not logged.") {
                alert("Your login period has expired! Please login again!");
                window.location.href = "../login.html";
            } 
            else if (respond != Type) {
            alert("You cannot access this page using your account!");
             if (respond == "admin") {
                 window.location.href = "../page/admin-system-management.php";
             } else if (respond == "student"){
                 window.location.href = "../page/student-dashboard.php";
             } else if (respond == "teacher"){
                 window.location.href = "../page/teacher-dashboard.php";
             }
         }
        }
    }
    // var Request = new XMLHttpRequest();
    // Request.open("GET", "../php/redirect.php", true);
    // Request.send();
    // Request.onload = function() {
     //     var respond = Request.responseText;
     //     if (respond == "not logged.") {
     //         alert("Your login period has expired! Please login again!");
     //         window.location.href = "../login.html";
     //     } else if (respond != Type) {
     //        alert("You cannot access this page using your account!");
     //         if (respond == "admin") {
     //             window.location.href = "../page/admin-system-management.php";
     //         } else if (respond == "student"){
     //             window.location.href = "../page/student-dashboard.php";
     //         } else if (respond == "teacher"){
     //             window.location.href = "../page/teacher-dashboard.php";
     //         }
     //     }
     // }
}

This is my checklogin.js file:

function login() {
    var enterID = document.getElementById("enterID").value;
    var password = document.getElementById("password").value;
 if ((password != "") && (enterID != "")) {

 var info = "?enterID=" + enterID + "&password=" + password;
 
 $.ajax
({
  type: "GET",
  url: "php/login.php"+info,
  success: function(respond)
  {
  
            if (respond == "admin") {
                window.location.href = "page/admin-system-management.php";
            } else if (respond == "student"){
                window.location.href = "page/student-dashboard.php";
            } else if (respond == "teacher"){
                window.location.href = "page/teacher-dashboard.php";
            } else{
                document.getElementById("errorMessage").innerText = respond;
            }  
  
 
  }
});
}
     else {
        document.getElementById("errorMessage").innerText = "Please fill in all the fields.";
    }
}

function redirect() {

 $.ajax
({
  type: "GET",
  url: "php/redirect.php",
  success: function(respond)
  {
  
                 if (respond != "not logged.") {
            if (respond == "admin") {
                window.location.href = "page/admin-system-management.php";
            } else if (respond == "student"){
                window.location.href = "page/student-dashboard.php";
            } else if (respond == "teacher"){
                window.location.href = "page/teacher-dashboard.php";
            }
        }
  
 
  }
});
}

And this is my login.php file:

<?php
session_start();
        include "mysql-connect.php";
        
        //get Info from login.html
        $ID = $_GET['enterID'];
        $PW = $_GET['password'];
        $stmt = $connect->prepare("SELECT PW, userType, nickName FROM users WHERE ID = ?");
        $stmt->bind_param("s",$ID);
        $valid = $stmt->execute();
        if (!$valid){
            die("Could not successfully run query.". $connect->connect_error);
        }
        $result = $stmt->get_result();
        if ($result->num_rows==0){
            //display message of no such student/teacher/admin
            echo "Failed to find an account with the input ID.";
        } else {
            $row = $result->fetch_assoc();
            if ($PW == $row['PW']) {
                $type = $row['userType'];
                $nick = $row['nickName'];
                //save data, record cookie for 6hours
                $_SESSION["type"]=$type;
                $_SESSION["userID"]=$ID;
                $_SESSION["nickName"]=$nick;
                //login success - Request.responseText to checklogin.js
                echo $type;

            } else {
                //display message of password error
                echo "The input password does not match the account password.";
            }
        }
        $connect->close();
?>

What am I doing wrong?