【问题标题】:How can I use a php file to display an HTML menu div in other HTML pages?如何使用 php 文件在其他 HTML 页面中显示 HTML 菜单 div?
【发布时间】: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 页面时看到&lt;?php include 'menu.php' ?&gt; 字符串,请关注@NicholasAnderson 并尝试将文件重命名为.php。但如果您的页面中只有&lt;div class="menu"&gt; &lt;/div&gt;,我们应该进行更多调查

标签: php html wamp


【解决方案1】:

浏览器正在呈现 HTML,因为这就是您给它的 index.html。为了让您的 wamp 服务器处理 menu.php。您需要将 index.html 上的扩展名更改为 .php。基本的 HTML 不知道是什么。

【讨论】:

  • 谢谢。因此,使用 WAMP,您可以仅使用编写一些 HTML 的 php 文件来提供整个 Web 应用程序吗?您还可以在 WAMP 设置中使用 HTML 文件和 JavaScript 吗?比如如果我想使用 HTML 作为表单,然后有一些 JavaScript 调用 PHP 文件或 PHP 函数?
  • 不一定,你的 index.html 可以看起来和它完全一样,主要是 html。当您需要动态元素或外部数据时,您可以像调用 menu.php 一样放入 php sn-p。这本书救了我上大学。 amazon.com/gp/aw/d/0672329166/ref=redir_mdp_mobile/…
  • 所以我可以有一个完全是 html 但带有 php 扩展名的索引文件,以防我需要动态元素或外部数据?我可以这样构建整个网页吗?我可以拥有只有 HTML 和一些 JavaScript 的 php 文件?谢谢推荐书,我去看看!
  • ...或者只是指示 Apache 将 .html 文件视为 PHP 到 .htaccess,如果 OP 想要保留 index.html 作为文件名。
  • IMO,只有基本的初步知识,进入 Apache 配置只会增加混乱。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 2019-07-08
  • 2023-03-03
  • 1970-01-01
  • 2012-03-11
  • 2013-06-14
  • 2018-06-23
  • 2017-12-01
相关资源
最近更新 更多