简体   繁体   English

从 HTML 文件链接到 PHP 文件

[英]Link to PHP file from a HTML File

I have created an header.html includes file as a menu structure that i use in my index.php.我创建了一个header.html包含文件作为我在index.php 中使用的菜单结构。

The problem is, when i click on the link in the index.php it directs it to the file with an .html extension - which actually does not exsist on the live server.问题是,当我单击index.php中的链接时,它会将其定向到扩展名为.html的文件 - 实际上在实时服务器上并不存在。 On my live server, i get an Error 404 page as a result.结果,在我的实时服务器上,我得到一个错误 404 页面。 When i type the URL with the correct extension (In that case: PlayerReg.php ), it directs me to the correct adress and it works.当我输入带有正确扩展名的 URL 时(在这种情况下: PlayerReg.php ),它会将我定向到正确的地址并且它有效。 But if i click on the link, i get the error Page.但是如果我点击链接,我会收到错误页面。

This is the index.php这是index.php

<!DOCTYPE html>
<html lang="en">
<head>
<?php include("includes/head.html"); ?>
</head>
<header>
<?php include("includes/header.html"); ?>
</header>
<body>
 

    
</body>
</html>

This the header.html这个header.html

<!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.0">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="btn-styles.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Palanquin+Dark&display=swap" rel="stylesheet"> 
    <title>Chess Team APP</title>
</head>

<header>
    <div class="row">
        <div class="col-md-3 col-sm-3 col-xs-6"> <a href="#" class="btn btn-sm animated-button sandy-one">Log-In</a> </div>
        <div class="col-md-3 col-sm-3 col-xs-6"> <a href="#" class="btn btn-sm animated-button sandy-two">Register</a> </div>
        <div class="col-md-3 col-sm-3 col-xs-6"> <a href="PlayerReg.php" class="btn btn-sm animated-button sandy-three">Register a new Player</a> </div>
        <div class="col-md-3 col-sm-3 col-xs-6"> <a href="#" class="btn btn-sm animated-button sandy-four">Learn more</a> </div>
      </div>
      
</header>

So on the site: http://codeplace.primebyte.ch/ when i click on the "Register A New Player" it should direct me to the registration form.所以在网站上: http://codeplace.primebyte.ch/当我点击“Register A New Player”时,它应该会引导我进入注册表。

I made sure i uploaded the correct file via FTP.我确定我通过 FTP 上传了正确的文件。

I would like to add that the head.html file works just fine but there are no links included.我想补充一点,head.html 文件工作正常,但不包含任何链接。 I am not even sure if it makes sense that the head is included in the head.html and the header.html我什至不确定将头部包含在 head.html 和 header.html 中是否有意义

For your markup to be valid using the combination of files that you have indicated you would need to modify your index.php page to be similar to the following:为了使您的标记使用您指定的文件组合有效,您需要将index.php页面修改为类似于以下内容:

In your code you are using includes/head.html etc means that the includes directory is a sub-directory of the current working directory (where the index.php page is) - if the files are located in a different directory they will not be included and will cause an error.在您使用的代码中includes/head.html等意味着includes目录是当前工作目录的子目录(index.php 页面所在的位置)- 如果文件位于不同的目录中,则它们不会包含并会导致错误。 By using __DIR__. '/includes'通过使用__DIR__. '/includes' __DIR__. '/includes' you provide a full path for PHP to use to find the files and using set_include_path allows you to simply include the file by name once this path is set - small timesaver perhaps. __DIR__. '/includes'为 PHP 提供完整路径以用于查找文件,使用set_include_path允许您在设置此路径后简单地按名称包含文件 - 也许可以节省时间。

<?php
    error_reporting(E_ALL);

    set_include_path( __DIR__ . '/includes' );
    require 'head.html';
?>
    <body>
<?php
    require 'header.html';
?>
        <h1>Hello World!</h1>
    </body>
</html>

As the HTML declaration and headers are all contained in head.html that should be the first content included.由于 HTML 声明和标头都包含在head.html中,因此应该是第一个包含的内容。

#head.html #head.html

<!-- head.html -->
<!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.0" />
        <link rel="stylesheet" href="style.css" />
        <link rel="stylesheet" href="btn-styles.css" />
        <link rel="preconnect" href="//fonts.googleapis.com" />
        <link rel="preconnect" href="//fonts.gstatic.com" crossorigin />
        <link href="//fonts.googleapis.com/css2?family=Palanquin+Dark&display=swap" rel="stylesheet" /> 
        <title>Chess Team APP</title>
    </head>

As the header section is HTML content it should be within the body - so the header.html file should be included after the body has been opened.由于header部分是 HTML 内容,它应该在正文中 - 因此header.html文件应该在body打开后包含在内。

#header.html #header.html

    <!-- header.html -->
    <header>
        <div class="row">
            <div class="col-md-3 col-sm-3 col-xs-6"><a href="#" class="btn btn-sm animated-button sandy-one">Log-In</a></div>
            <div class="col-md-3 col-sm-3 col-xs-6"><a href="#" class="btn btn-sm animated-button sandy-two">Register</a></div>
            <div class="col-md-3 col-sm-3 col-xs-6"><a href="PlayerReg.php" class="btn btn-sm animated-button sandy-three">Register a new Player</a></div>
            <div class="col-md-3 col-sm-3 col-xs-6"><a href="#" class="btn btn-sm animated-button sandy-four">Learn more</a></div>
          </div>
    </header>

when rendered in the browser this will yield:在浏览器中呈现时,这将产生:

<!-- head.html -->
<!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.0" />
        <link rel="stylesheet" href="style.css" />
        <link rel="stylesheet" href="btn-styles.css" />
        <link rel="preconnect" href="//fonts.googleapis.com" />
        <link rel="preconnect" href="//fonts.gstatic.com" crossorigin />
        <link href="//fonts.googleapis.com/css2?family=Palanquin+Dark&display=swap" rel="stylesheet" /> 
        <title>Chess Team APP</title>
    </head>
    <body>

        <!-- header.html -->
        <header>
            <div class="row">
                <div class="col-md-3 col-sm-3 col-xs-6"><a href="#" class="btn btn-sm animated-button sandy-one">Log-In</a></div>
                <div class="col-md-3 col-sm-3 col-xs-6"><a href="#" class="btn btn-sm animated-button sandy-two">Register</a></div>
                <div class="col-md-3 col-sm-3 col-xs-6"><a href="PlayerReg.php" class="btn btn-sm animated-button sandy-three">Register a new Player</a></div>
                <div class="col-md-3 col-sm-3 col-xs-6"><a href="#" class="btn btn-sm animated-button sandy-four">Learn more</a></div>
              </div>
        </header>
        <h1>Hello World!</h1>
    </body>
</html>

Everything is in the correct place一切都在正确的地方

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

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