简体   繁体   中英

Why aren't my php classes working after an ajax call

This is my index.php file when the user isn't logged in it loads an login page else it loads the app

$user = new User();
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Page Title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="stylesheet" type="text/css" media="screen" href="assets/css/master.css"/>
        <script src="assets/js/functions.js?<? filectime('assets/js/functions.js') ?>"></script>

        <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

        <script src="assets/vendor/jquery-validation/jquery.validate.min.js"></script>
        <script src="assets/vendor/jquery-validation/additional-methods.min.js"></script>

        <link rel="stylesheet" href="assets\vendor\bootstrap\dist\css\bootstrap.css">
    </head>
    <body>
        <div class="main-wrapper container-fluid">
        <?
        if($user->isLoggedIn()) {
            include_once 'app/index.php';
        } else {
            include_once 'files/auth/login.php';
        }
        ?>
        </div>
    </body>
</html>

This is the login.php file that gets loaded in whe the user is not logged in

 <div class="login-card">
    <form id="sign-in-form">
        <h2>Sign in</h2>
        <div class="form-group">
            <input type="text" name="username" required class="form-control" placeholder="Username">
            <input type="password" name="password" required class="form-control" placeholder="Password">
            <button type="submit" class="btn btn-primary btn-lg btn-block">sign in</button>
        </div>
    </form>
</div>
<div class="login-links">
    <a href="" id="register-link">Create an account</a>
    <a href="" class="grid-justify-end">Forgot password</a>
</div>

<script src="files/auth/js/login.js?<?= filemtime('files/auth/js/login.js') ?>"></script>

When a user clicks on create an account an Ajax function gets called and it loads the register page in the main-wrapper div but when it gets loaded in my php class User is undefiend? and the variable $user in index.php is also undefiend

尝试在注册文件中包含包含用户类的文件。

Like this include 'path/to/users_phph_file/Users.php'; $user = new User(); include 'path/to/users_phph_file/Users.php'; $user = new User();

if you folder structure looks like this:

root -classes --users.php index.php

then include 'classes/Users.php'; $user = new User(); include 'classes/Users.php'; $user = new User();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM