简体   繁体   English

在html页面中包含php文件

[英]Include php file in html page

I'm working with XML and PHP to populate a web form drop down box. 我正在使用XML和PHP填充Web表单下拉框。

I have a html page 我有一个html页面

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>

</head>

<body>
<header>
<h1>Title</h1>
</header>

    <form>
        <div id ="select xml">
            <?php include 'dropdown.php'; ?>
        </div>
    </form>
</body>

</html>

and I am hoping to include the following PHP to generate the actual box with the file names of the XML. 并且我希望包含以下PHP以使用XML的文件名生成实际的框。

<?php
//echo(substr(glob("xml/*.xml")[0],4));

echo  "<p>
      <label>Select list</label><br>
      <select id = \"selectxml\">
      <option value'0'>--Please Select--</option>";

        $count = count(glob("xml/*.xml"));
        $files = glob("xml/*.xml");
        for ($i = 0; $i < $count; $i++) {
            //echo(substr($files[$i],4));
            //echo "<br>";
        $filename = (substr($files[$i],4));
        echo "<option value=$i+1>$filename</option>";   
        }     

echo  "</select><br>
      <br>
      </p>";
?>

I'm aware the PHP isnt perfect, but it works. 我知道PHP并不是完美的,但是它可以工作。

The issue is that the Include HTML does not work when I run the page - any ideas? 问题是,当我运行页面时,“包含HTML”不起作用-有什么想法吗?

Change the extension to .php and you will be okay . 将扩展名更改为.php ,您就可以了。

When the page have .html extension, the web server doesn't recognize it as a PHP file, and you can't use PHP codes in it. 页面扩展名为.html时 ,Web服务器不会将其识别为PHP文件,并且您不能在其中使用PHP代码。 and any PHP code, will be processed as a plain text . 以及任何PHP代码,都将作为纯文本进行处理。

I mean when you put : 我的意思是,当您放置:

<?php echo "hello" ?>

in a HMTL page, browser will show : HMTL页面中,浏览器将显示:

<?php echo "hello" ?>

But, if the page have .php extension, browser will show : 但是,如果页面扩展名为.php ,浏览器将显示:

hello

将您的html页面从.html重命名为.php

这是不可能的,但是使用jQuery.load函数,您可以这样做:

$("#select-xml").load("dropdown.php");

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

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