我在包含一个包含一些js脚本库的文件时遇到了问题。
index.php
<?php
include 'header.php';
?>
<section>
<div>
<p>misc. html</p>
</div>
</section>
<?php
include ($_SERVER['DOCUMENT_ROOT'].'/footer.php');
echo 'test ='.$testVar;
?>
footer.php
<?php
include ($_SERVER['DOCUMENT_ROOT'].'/includes/footerscripts.php');
?>
</body>
</html>
/包括/footerscripts.php
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<?php
$testVar = 'test';
?>
在这种情况下,索引文件在运行时的源代码中确实包含以下内容:
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
test =
脚本正在到达那里,但没有被执行,变量也没有通过。
当我将footer.php更改为直接包含这些脚本和变量时,变量会变为index.php,脚本也会变为,但它们没有执行(或者没有按时执行?)。
有什么想法吗?
我有一个奇怪的php5.ini文件,因为我不知道该放什么,如果这有什么不同的话。include文件路径似乎还可以,因为它在大多数情况下仍然有效。
谢谢,马特
编辑:添加了include('header.php'),它工作得很好,可以拉入我的头。
该文件看起来像:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="--">
<meta name="keywords" content="--">
<meta name="author" content="--">
<title>--</title>
<link rel="stylesheet" href="/css/mainstylesheet.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="icon" href="/images/favicon.ico">
</head>
<body>
<header class="nav-top" id="top" role="navigation">
<div class="container">
<div class="home-link">
<a href="../"><img src="/images/logo/logo-p.png" alt="plc" class="logo" /></a>
</div>
<nav class="nav-main">
<ul>
<li>
<a href="../">Home</a>
</li>
<li>
<a href="/portfolio">Portfolio</a>
</li>
<li>
<a href="/contact">Contact</a>
</li>
</ul>
</nav>
</div>
</header>
编辑:我想我只是需要从文档的标题加载jquery,因为它是这样工作的。我仍然不能将变量从孙子带到父,但哦,好吧
根据您的示例,index.php
文件缺少一些东西:
<?php
include ($_SERVER['ROOT'].'/footer.php');
echo 'test ='.$testVar;
?>
我假设你没有发布整个文件,因为这里的输出hmtl无效-缺少html
打开标记等。你能提供整个和/或简化的索引文件吗?
用$_SERVER['DOCUMENT_ROOT']
替换$_SERVER['ROOT']
的所有实例。