简体   繁体   English

PHP标头()重定向后未加载JavaScript

[英]JavaScript not loading after PHP header() redirect

I have a page where a user can sign in by submitting a basic form, the data is sent to a separate PHP script (for validation, etc). 我有一个页面,用户可以通过提交基本表单来登录,该数据将发送到单独的PHP脚本(用于验证等)。 The PHP script ends with the header() function to direct the user to a welcome page: PHP脚本以header()函数结尾,以将用户定向到欢迎页面:

// form validation above
header(Location: ../welcome.php);
exit();

I would like to be able to run some JavaScript on the welcome page, however I am noticing that even this basic code won't fire: 我希望能够在欢迎页面上运行一些JavaScript,但是我注意到即使是基本代码也不会触发:

$(document).ready(function() {
    console.log("ready!");
});

I have split the welcome page using PHP include to separate the header, body, and footer: 我使用PHP include拆分了欢迎页面,以分隔页眉,正文和页脚:

The Header: 标头:

<?php 
session_start();
?>

<!DOCTYPE html>
<html>
<head>
    <!-- HOSTED ONLINE -->
    <!-- Google Fonts: Lato & Pacifio -->
    <link href="https://fonts.googleapis.com/css?family=Lato|Pacifico" rel="stylesheet">
    <!-- Font Awesome -->
    <script src="https://use.fontawesome.com/3d58889dd8.js"></script>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <!-- Bootstrap jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <!-- Bootstrap JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <!-- jQuery -->
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <!-- jQuery UI -->
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    <!-- LOCAL -->
    <!-- CSS -->
    <link rel="stylesheet" type="text/css" href="resources/style.css">
    <!-- JavaScript -->
    <script type="text/javascript" src="resources/script.js"></script>
    <!-- Animate.css -->
    <link rel="stylesheet" href="resources/animate.css">

    <title>Website Title</title>
</head>

<body class="userBody">
    <!-- title card -->
    <div class="container-fluid titleCard">
        <h1 class="pacificoFont mainTitle">Website Title</h1>
    </div>

The Body: 身体:

<?php
include_once "user_header.php";
?>

<div>
    <?php
    echo "welcome " . $_SESSION["username"] . "!<br>";
    echo "Your email is " . $_SESSION["email"];
    echo '<form action="includes/logout_inc.php" method="POST">
    <button type="submit" name="submit">Logout</button></form>';
    ?>
</div>

<div class="test" id="test">
    Test
</div>

<?php
include_once "user_footer.php";
?>

The Footer: 页脚:

</body>
</html>

I do believe the local JavaScript is properly linked, where it's in the same directory as the CSS and the styling is linked and working. 我确实相信本地JavaScript已正确链接,并且与CSS位于同一目录中,并且样式已链接并且可以正常工作。 I am also running this locally using MAMP (if that info helps). 我也正在使用MAMP在本地运行(如果该信息有帮助)。

It's probably quite noticeable that I am very new at PHP/JavaScript. 我是PHP / JavaScript的新手,这也许很值得注意。 Any help would be deeply appreciated! 任何帮助将不胜感激! Thank you for taking the time :) 谢谢您抽出宝贵的时间:)

the javascript reference should be at the bottom of the HTML page before the closing body tag. javascript参考应该在HTML页面的底部,位于body标记之前。 Just cut the reference of the javascript to the the page bottom below: 只需将javascript的引用剪切到下面的页面底部:

<script type="text/javascript" src="resources/script.js"></script>
</body>
</html>

The problem is that jquery got rendered before your page load. 问题是在页面加载之前呈现了jquery。

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

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