如何使用 php 文件在其他 HTML 页面中显示 HTML 菜单 div?
How can I use a php file to display an HTML menu div in other HTML pages?
我有一个 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 文件的页面中?有没有更好的方法来制作页面并在每个页面中都有一个菜单?我很感激任何朝着正确方向的推动,也许是为了学习这类东西的教科书或良好的文档来源。到目前为止我发现的教程有类似的解决方案,但出于某种原因它对我不起作用。
浏览器正在呈现 HTML,因为那是你给它的,index.html。为了让您的 wamp 服务器处理 menu.php。您需要在 index.html 上将扩展名更改为 .php。基本HTML不知道是什么。
我有一个 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 文件的页面中?有没有更好的方法来制作页面并在每个页面中都有一个菜单?我很感激任何朝着正确方向的推动,也许是为了学习这类东西的教科书或良好的文档来源。到目前为止我发现的教程有类似的解决方案,但出于某种原因它对我不起作用。
浏览器正在呈现 HTML,因为那是你给它的,index.html。为了让您的 wamp 服务器处理 menu.php。您需要在 index.html 上将扩展名更改为 .php。基本HTML不知道是什么。