是否应该使Apache2服务器将.html文件视为/视为/解析为/ php文件?

时间:2019-05-27 08:24:13

标签: php html

我最近开始尝试使用PHP。

我已经在Linux Mint 19.1上安装了带有PHP 7.2的Apache2服务器。 然后,我在AddType application/x-httpd-php .html .htm中添加了mime.conf,以便我的服务器将.html视为PHP。

但是我不知道这是否是正确的方法。 目前,我在var / www / mysite中有2个文件:index.html和test.php。 我已将DocumentRoot /var/www/mysite添加到/etc/apache2/sites-enabled/000-default.conf

我的.html是这样的:

<!DOCTYPE html>
<html>    
<?php include('test.php'); ?>
<head>
    <meta charset = "utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">    
    <link rel = "stylesheet" type = "text/css" href = "master.css">   
    <title>Home Page</title>
    <script type="text/javascript" src="jquery-1.12.4.min.js"></script>
<script>
$( document ).ready(function() {
    $('.URR').change(function() {
        var n = $('.URR').val();
        if (n < 1)
            $('.URR').val(1);
        if (n > 10)
            $('.URR').val(10);
    });
    $('.Brightness').change(function() {
        var m = $('.Brightness').val();
        if (m < 2)
            $('.Brightness').val(2);            
        if (m > 99)
            $('.Brightness').val(99);
    });
});
</script>
</head>

<body>

    <div id = "container">
        <div class = "center-container">
            <div class = "logo"><img url="/logo.png"></div>
            <div class = "title"><h1>Current Settings</h1></div>
            <div id = "center_text_box">
                <p>URL:</p> <p><span>%PLACEHOLDER_URL%</span></p>
                <p>URL Refresh Rate:</p><p><span>%PLACEHOLDER_URR%</span>(s)</p>
                <p>Brightness:</p> <p><span><?php if(isset($Brightness)) print $Brightness; ?></span>(&#37;)</p>
            </div>
        </div>
    </div>
</body>
</html>

而test.php是这样的:

<?php
// Check if the form is submitted 
if ( isset( $_POST['save_values'] ) ) { // retrieve the form data by using the element's name attributes value as key 
    $Brightness = $_POST['getBrightness'];
    $RefreshRate = $_POST['getRefreshRate'];

} 
?>

这是一个好习惯吗?

在{.1文件中包含<?php include('test.php'); ?>并在.html文件中需要的地方使用<?php ?>标签吗?

或者我应该停止将.html解析为.php,而是开始通过以下操作在PHP文件中生成HTML:

<?php
$data = "<!DOCTYPE html>
<html>
<head>
    <meta charset = "utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">    
    <link rel = "stylesheet" type = "text/css" href = "master.css">   
    <title>Home Page</title>
    <script type="text/javascript" src="jquery-1.12.4.min.js"></script>
<script>
$( document ).ready(function() {
    $('.URR').change(function() {
        var n = $('.URR').val();
        if (n < 1)
            $('.URR').val(1);
        if (n > 10)
            $('.URR').val(10);
    });
    $('.Brightness').change(function() {
        var m = $('.Brightness').val();
        if (m < 2)
            $('.Brightness').val(2);            
        if (m > 99)
            $('.Brightness').val(99);
    });
});
</script>
</head>

<body>
    <div id = "container">
        <div class = "center-container">
            <div class = "logo"><img url="/logo.png"></div>
            <div class = "title"><h1>Current Settings</h1></div>
            <div id = "center_text_box">
                <p>URL:</p> <p><span>%PLACEHOLDER_URL%</span></p>
                <p>URL Refresh Rate:</p><p><span>%PLACEHOLDER_URR%</span>(s)</p>
                <p>Brightness:</p> <p><span>
"
if(isset($Brightness)) print $Brightness;
$data .= "$Brightness</span>(&#37;)</p>
            </div>
        </div>
    </div>
</body>
</html>
"

echo $data;
?>

如果以后是“解决之道”,我是否可以有多个.php文件来做事情? 以及他们如何一起工作?

还可以在$data内部使用JavaScript或jQuery吗?它可以在PHP内部的HTML内部工作吗?

0 个答案:

没有答案