【发布时间】:2015-01-31 18:17:14
【问题描述】:
我有一个 index.html 文件、一个 menu.php 文件和一个 testing.php 文件。我正在开发一个简单的 Web 应用程序,用于将联系人存储到数据库中,并(最终)在我的服务器上以格式良好的 html 页面显示它们。我正在使用 WAMP 堆栈来学习。经过大量的试验和错误,我终于可以访问我的数据库并且可以插入新的联系人。不过,我希望能够在每个页面的顶部显示一个导航菜单,以便在添加联系人后返回主页。这样我就可以添加任意数量的联系人。
这里是 index.html 文件:
<!DOCTYPE html>
<html>
<body>
<div class="menu">
<?php include 'menu.php' ?>
</div>
<form action="testing.php" method="post">
Name: <input type="text" name="name"></br>
Phone Number: <input type='text' name="number"></br>
<input type="submit">
</form>
</body>
</html>
这里是 menu.php 文件:
<style type = "text/css">
.menu{
position: absolute;
width: 50px;
float:right;
height: 20px;
}
</style>
<div class = "menu">
<?php
echo '<a href="index.html">Home</a> -
<a href ="/test.php">Test</a> -
<a href ="http:/www.google.com">Google</a>';
?>
</div>
这是写入数据库的 testing.php 文件:
<?php
$name = $_POST["name"];
$phonenumber = $_POST["number"];
$data = array('name' => $name, 'phonenumber' => $phonenumber);
try {
echo '<h1>Attempting connection to database.</h1>';
$conn = new PDO('mysql:host=localhost;dbname=TestDev', "root", "pass*");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$STH = $conn -> prepare("INSERT INTO tblContacts (Name, PhoneNumber) VALUES (:name, :phonenumber)");
$STH -> execute($data);
}catch(PDOException $e) {
echo '<h1>Not connected to database.</h1>';
$connectedBool = false;
}
?>
为什么菜单没有显示在我包含 php 文件的页面中?有没有更好的方法来制作页面并在每个页面中都有一个菜单?我会很感激朝着正确的方向轻推,也许是朝着教科书或学习此类事物的良好文档来源。到目前为止,我发现的教程有类似的解决方案,但由于某种原因它对我不起作用。
【问题讨论】:
-
把你的 index.html 改成 index.php
-
如果您在浏览
index.html页面时看到<?php include 'menu.php' ?>字符串,请关注@NicholasAnderson 并尝试将文件重命名为.php。但如果您的页面中只有<div class="menu"> </div>,我们应该进行更多调查