简体   繁体   English

如何使用php文件在其他HTML页面中显示HTML菜单div?

[英]How can I use a php file to display an HTML menu div in other HTML pages?

I have an index.html file, a menu.php file, and a testing.php file. 我有一个index.html文件,一个menu.php文件和一个testing.php文件。 I'm working on a simple web application for storing contacts into a database and (eventually) displaying them in a nicely formatted html page on my server. 我正在开发一个简单的Web应用程序,用于将联系人存储到数据库中,并(最终)在服务器上以格式正确的html页面显示联系人。 I'm using the WAMP stack to learn. 我正在使用WAMP堆栈进行学习。 After a lot of trial and error, I've finally got access to my database and I can insert new contacts. 经过大量的试验和错误,我终于可以访问数据库,并且可以插入新的联系人。 I want to be able to display a navigation menu at the top of every page, though, so that I can go back to the main page after adding a contact. 不过,我希望能够在每个页面的顶部显示一个导航菜单,以便在添加联系人后可以返回主页。 That way I can add as many contacts as I want. 这样,我可以添加任意数量的联系人。

Here is the index.html file: 这是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>

Here is the menu.php file: 这是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>

Here is the testing.php file that writes to the database: 这是写入数据库的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;
}
?>

Why doesn't the menu show up in the pages I've included the php file in? 为什么菜单没有显示在我包含php文件的页面中? Is there a better way to make pages and have a menu in each page? 有没有更好的方法来制作页面并在每个页面中都有一个菜单? I'd appreciate any nudge in the right direction, perhaps towards a textbook or good documentation source for learning this kind of thing. 我希望能向正确的方向轻描淡写,也许是为了学习此类事物而准备的教科书或良好的文档来源。 The tutorials I've found so far have a similar solution but for some reason it isn't working for me. 到目前为止,我发现的教程也有类似的解决方案,但是由于某种原因,它对我不起作用。

The browser is rendering HTML because that's what you're giving it, index.html. 浏览器正在呈现HTML,因为您正在提供HTML,即index.html。 In order for your wamp server to process the menu.php. 为了使您的Wamp服务器处理menu.php。 You need to change the extension to .php on index.html. 您需要在index.html上将扩展名更改为.php。 Basic HTML doesn't know what is. 基本的HTML不知道是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM