简体   繁体   English

为什么Ajax调用后我的php类无法正常工作

[英]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 当用户未登录时,这是我的index.php文件,它会加载登录页面,否则会加载应用程序

$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 这是在用户未登录时加载的login.php文件

 <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? 当用户单击创建帐户时,将调用Ajax函数并将其加载到main-wrapper div中的注册页面,但是当将其加载到我的php类中时,用户是否感到困惑? and the variable $user in index.php is also undefiend 并且index.php中的变量$ user也没有防御

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

Like this include 'path/to/users_phph_file/Users.php'; $user = new User(); 像这样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 root -classes --users.php index.php

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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