-2

我正在为我的网站编写一个 PHP 脚本,该脚本从表单中获取信息,对其进行计算,然后在新页面中返回结果。程序本身所做的不是这里的问题,而是如何格式化输出。下面是我的程序中最终将内容写入网页的部分:

.... code ....

$output = <<< EOHTML
<?php require_once "../includes/header.htm"; ?>
        <title>$var1</title>
    </head>
    <body>
        <div id="container">
            <?php include_once "../includes/banner.htm";
                require_once "../includes/menu.htm"; ?>
            <div class="content">
                <h1>Test</h1>

                <p>Variable 1: $var1</p>
                <p>Variable 2: $var2</p>
                <p>Variable 3: $var3</p>
                <p>Variable 4: $var4</p>
                <p>Variable 5: $var5</p>
                <p>Variable 6: $var6</p>

            </div>
            <?php include_once "../includes/footer.htm" ?>
        </div>
    </body>
</html>
EOHTML;

print($output);
?>

然而,虽然 HTML 工作正常,但它似乎按字面意思打印 PHP 部分,而服务器无法解析它。各种“包含”用于加载我网站的部分内容,包括 CSS,因此输出只有 6 行白色背景上的文本。

有没有办法让它输出 HTML 和 PHP(并且都正确解析)或将 PHP 输出到表单所在的同一页面?

4

2 回答 2

1

试试这个:

<html>
<head>
<?php require_once "../includes/header.htm"; ?>
    <title><?php echo $var1; ?></title>
</head>
<body>
    <div id="container">
        <?php include_once "../includes/banner.htm";
            require_once "../includes/menu.htm"; ?>
        <div class="content">
            <h1>Test</h1>

            <p>Variable 1: <?php echo $var1; ?></p>
            <p>Variable 2: <?php echo $var2; ?></p>
            <p>Variable 3: <?php echo $var3; ?></p>
            <p>Variable 4: <?php echo $var4; ?></p>
            <p>Variable 5: <?php echo $var5; ?></p>
            <p>Variable 6: <?php echo $var6; ?></p>

        </div>
        <?php include_once "../includes/footer.htm" ?>
    </div>
</body>
</html>

php 没有调用您的变量,因此 html 将其作为文字输出。

于 2013-11-06T16:55:04.137 回答
0

Apart from how to use Variables in HTML parts of your code (See Answer of iamde_coder) you saved your PHP files as xxxxx.htm files. Your Webserver will not execute any such file when called but rather return it's contents, including any PHP source code.

Store your files as xxxxx.php files if they contain any PHP code and your webserver will execute them!

You might also want to read some more tutorials before diving deeper into any more application logic too.

于 2013-11-06T16:58:50.917 回答