PHP头文件()重定向后没有加载JavaScript [英] JavaScript not loading after PHP header() redirect
问题描述
我有一个页面,用户可以通过提交基本表单登录,数据被发送到单独的PHP脚本(用于验证等)。 PHP脚本以header()函数结束,将用户引导到欢迎页面:
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). The PHP script ends with the header() function to direct the user to a welcome page:
// form validation above
header(Location: ../welcome.php);
exit();
我希望能够在欢迎页面上运行一些JavaScript,但我注意到它甚至这个基本代码不会触发:
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:
$(document).ready(function() {
console.log("ready!");
});
我已经使用PHP include拆分欢迎页面来分隔页眉,正文和页脚:
I have split the welcome page using PHP include to separate the header, body, and footer:
标题:
<?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>
正文:
<?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";
?>
页脚:
</body>
</html>
我相信本地JavaScript已正确链接,它与CSS位于同一目录中造型是联系和工作的。我也在本地使用MAMP运行(如果该信息有帮助)。
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. I am also running this locally using MAMP (if that info helps).
我可能非常注意到我是PHP / JavaScript的新手。任何帮助将深表感谢!感谢您抽出宝贵时间:)
It's probably quite noticeable that I am very new at PHP/JavaScript. Any help would be deeply appreciated! Thank you for taking the time :)
推荐答案
javascript参考应该在关闭正文标记之前的HTML页面底部。只需将javascript的引用剪切到下面的页面底部:
the javascript reference should be at the bottom of the HTML page before the closing body tag. Just cut the reference of the javascript to the the page bottom below:
<script type="text/javascript" src="resources/script.js"></script>
</body>
</html>
问题是在页面加载之前渲染了jquery。
The problem is that jquery got rendered before your page load.
这篇关于PHP头文件()重定向后没有加载JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!