【问题标题】:php echo entire html without using readfilephp 在不使用 readfile 的情况下回显整个 html
【发布时间】:2015-01-23 01:28:42
【问题描述】:

顾名思义,是否可以回显整个 html 和 css? 我正在使用 if 并且每次匹配都需要生成不同的页面。

另外,在 html 代码中的 html 代码中,我需要回显包含在 div 中并设置样式的变量。

有什么想法可以做到这一点吗?

我收到以下错误语法错误,意外的 T_STRING,期待

谢谢

    echo '<!DOCTYPE html><html lang="mk">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

            <style type="text/css">
            @import url(http://fonts.googleapis.com/css?family=Roboto&subset=cyrillic);
            @import url(http://fonts.googleapis.com/css?family=Open+Sans&subset=cyrillic);
            @import url(http://fonts.googleapis.com/css?family=Fjalla+One);

            *:focus { outline: none; }
            ::-webkit-scrollbar { width: 0; }
            .animate { -webkit-transition: all 0.2s ease-in-out; -moz-transition: all 0.2s ease-in-out; -o-transition: all 0.2s ease-in-out; transition: all 0.2s ease-in-out; }

            body { min-width: 300px; width: auto; max-width: 1100px; height: auto; margin: 0 auto; padding: 0; background-color: white; }

            div#container {
                position: relative; float: left; clear: none;
                width: 100%; height: auto; margin: -3px 0 30px 0; padding: 0 0 50px 0;
                background-image: url(guide.jpg); background-position: center bottom; background-repeat: no-repeat; background-size: cover;
                background-color: pink;
                -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px;
            }

            html body div#container form {
                position: relative; float: left; clear: none;
                width: 100%; height: auto; margin: 40px 0 0 0; padding: 0;
            }

            html body div#container form div#email-container {
                position: relative; float: left; clear: none; display: block;
                width: 100%; height: auto; margin: 0 auto; padding: 0; overflow: hidden;                
                text-align: center;
            }
            html body div#container form div#email-container div#email-input {
                position: relative; float: none; clear: none; display: inline-block;

                width: auto; height: auto; margin: 0 auto; padding: 25px 40px;
                font-family: 'Fjalla One', sans-serif; font-weight: 400; font-size: 40px; letter-spacing: normal;

                -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
                background-color: rgba(26, 60, 88, 0.9); color: #ffffff;
                cursor: default;
            }


            label.styled {
                position: relative; float: left; clear: none; display: block;
                width: auto; height: auto; margin: 0; padding: 10px 20px; overflow: hidden;

                font-family: 'Roboto', sans-serif; font-weight: 400; font-size: 15px; letter-spacing: normal;

                -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
                background-color: #ffffff; color: rgba(128, 130, 132, 1); box-shadow: inset 0 0 0 2px #b6b6b8; 
                cursor: pointer;
            }

            label.styled:before {
                position: absolute; float: none; clear: both; top: 0; left: 0; display: block;
                width: 40px; height: 40px; margin: 0 0 0 10px; padding: 0; 

                content: '✔';
                font-size: 60px;

                opacity: 0;
            }

            input.styled {
                display: none;
            }

input.styled:checked ~ label.styled {
  padding-left: 50px;
  opacity: 1;

  background-color: rgba(128, 130, 132, 0.9); color: #ffffff; box-shadow: inset 0 0 0 2px rgba(128, 130, 132, 0.9);;
}
input.styled:checked ~ label.styled:before {
  top: -10px; left: -2px;
  opacity: 1; color: rgba(255, 255, 255, 0.4);
}


        </style>
</head>
<body>

    <div id="container" class="animate">


    <form method="post" action="$action" class="animate">

        <input type="hidden" name="mode_login">
        <input type="hidden" name="redirect" value="$redirect">
        <input type="hidden" name="accept_terms" value="yes">

            <div id="email-container">
            <div id="email-input">' . $_POST['fes-email'] . '</div>
            </div>

    </div>


    <input type="checkbox" class="styled animate" id="checkbox-tnc-accept">
    <label class="styled animate" for="checkbox-tnc-accept">Ги прифаќам Условите и правилата <br> за користење на овој сервис</label>

    <button type="submit" value="Enter">Продолжи</button>

    </form>




</body>
</html>';

【问题讨论】:

  • 简短的回答是:是的。你能告诉我们一些你的代码吗?
  • echo? include? file_get_contents?目前还不清楚你对readfile 有什么反对,为什么你需要readfile 或类似的东西,以及你首先真正遇到的问题。请举个例子。
  • 您也可以使用 ifs 并生成包含文件。
  • 不要在.字符串连接中使用额外的echo;
  • @Boris 帮助您的最简单方法是,如果您发布整个代码,请说明您现在得到的结果(+ 确切的错误消息)、您期望什么输出、您尝试过什么以及在哪里你被卡住了吗,所以我们可以向你展示错误并解释如何修复它以及发生了什么,以便下次你可以自己修复代码:D

标签: php html css echo


【解决方案1】:

我建议你不要用php打印html的东西,只要关闭php标签,写普通的html,所以这里我去掉了大的echo语句,写了普通的html。这使代码更干净,您可以更好地了解什么是 html 以及 PHP 在哪里。

所以你的php代码:

<div id="email-input">' . $_POST['fes-email'] . '</div>

如果您在 echo 语句中没有它,请使用:

<div id="email-input"><?= $_POST['fes-email'] ?></div>

这与以下内容几乎相同:

<div id="email-input"><?php echo $_POST['fes-email']; ?></div>

对于有用的错误消息,您可以使用以下行打开错误报告:

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?>

您可以将它们放在您的 php 文件之上,并确保它们仅在暂存时打开!

整个代码应该是这样的:

<!DOCTYPE html>
<html lang="mk">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

            <style type="text/css">

                @import url(http://fonts.googleapis.com/css?family=Roboto&subset=cyrillic);
                @import url(http://fonts.googleapis.com/css?family=Open+Sans&subset=cyrillic);
                @import url(http://fonts.googleapis.com/css?family=Fjalla+One);

                *:focus { outline: none; }
                ::-webkit-scrollbar { width: 0; }
                .animate { -webkit-transition: all 0.2s ease-in-out; -moz-transition: all 0.2s ease-in-out; -o-transition: all 0.2s ease-in-out; transition: all 0.2s ease-in-out; }

                body { min-width: 300px; width: auto; max-width: 1100px; height: auto; margin: 0 auto; padding: 0; background-color: white; }

                div#container {
                    position: relative; float: left; clear: none;
                    width: 100%; height: auto; margin: -3px 0 30px 0; padding: 0 0 50px 0;
                    background-image: url(guide.jpg); background-position: center bottom; background-repeat: no-repeat; background-size: cover;
                    background-color: pink;
                    -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px;
                }

                html body div#container form {
                    position: relative; float: left; clear: none;
                    width: 100%; height: auto; margin: 40px 0 0 0; padding: 0;
                }

                html body div#container form div#email-container {
                    position: relative; float: left; clear: none; display: block;
                    width: 100%; height: auto; margin: 0 auto; padding: 0; overflow: hidden;                
                    text-align: center;
                }
                html body div#container form div#email-container div#email-input {
                    position: relative; float: none; clear: none; display: inline-block;

                    width: auto; height: auto; margin: 0 auto; padding: 25px 40px;
                    font-family: 'Fjalla One', sans-serif; font-weight: 400; font-size: 40px; letter-spacing: normal;

                    -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
                    background-color: rgba(26, 60, 88, 0.9); color: #ffffff;
                    cursor: default;
                }


                label.styled {
                    position: relative; float: left; clear: none; display: block;
                    width: auto; height: auto; margin: 0; padding: 10px 20px; overflow: hidden;

                    font-family: 'Roboto', sans-serif; font-weight: 400; font-size: 15px; letter-spacing: normal;

                    -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
                    background-color: #ffffff; color: rgba(128, 130, 132, 1); box-shadow: inset 0 0 0 2px #b6b6b8; 
                    cursor: pointer;
                }

                label.styled:before {
                    position: absolute; float: none; clear: both; top: 0; left: 0; display: block;
                    width: 40px; height: 40px; margin: 0 0 0 10px; padding: 0; 

                    content: '✔';
                    font-size: 60px;

                    opacity: 0;
                }

                input.styled {
                    display: none;
                }

                input.styled:checked ~ label.styled {
                  padding-left: 50px;
                  opacity: 1;

                  background-color: rgba(128, 130, 132, 0.9); color: #ffffff; box-shadow: inset 0 0 0 2px rgba(128, 130, 132, 0.9);;
                }
                input.styled:checked ~ label.styled:before {
                  top: -10px; left: -2px;
                  opacity: 1; color: rgba(255, 255, 255, 0.4);
                }


        </style>

    </head>

    <body>

        <div id="container" class="animate">

        <form method="post" action="$action" class="animate">

            <input type="hidden" name="mode_login">
            <input type="hidden" name="redirect" value="$redirect">
            <input type="hidden" name="accept_terms" value="yes">

            <div id="email-container">
                <div id="email-input"><?= $_POST['fes-email'] ?></div>
            </div>

        </div>


            <input type="checkbox" class="styled animate" id="checkbox-tnc-accept">
            <label class="styled animate" for="checkbox-tnc-accept">Ги прифаќам Условите и правилата <br> за користење на овој сервис</label>

            <button type="submit" value="Enter">Продолжи</button>

    </form>




    </body>
</html>

编辑:

如果你在 if 语句中有这个 echo 语句,你仍然不需要那样打印它:D

作为一个例子(伪代码):

<?php

    if(condition) {
        echo "<div>HTML STUFF</div>";  //Don't do it that way
    } else {
?>

<div>Put your HTML stuff here</div>

<?php
    }
?>

如果你必须连接东西:

                   //v Your variable here
  "String is here" . $myVariable . " string goes further"
//^String start  ^ ^Concatenate  ^ ^                    ^String ends
              // |Break the string |

有关串联的更多信息,请参阅手册:http://php.net/manual/en/language.operators.string.php

我还建议您不要将 CSS 内联或在样式标签中编写。您可以使用 *.css 编写自己的 CSS 文件,然后将它们包含在 html 头标签中:

<link rel="stylesheet" type="text/css" href="yourCssFile.css">

【讨论】:

  • 我知道这样更好。但出于这个特定目的,我需要根据 IF 生成代码。
  • 我能以某种方式、形状或形式实现这一目标吗?
  • @Boris 更新了我的答案,希望它对您有所帮助,并且您了解一切!否则只要问有什么不清楚的地方
  • 全部清楚,谢谢...问题是我从 google 字体复制了我的 css 中的字体系列,它是单引号而不是双引号....
【解决方案2】:
echo '...assuming the previous part of the entire code are also present...
<form>    
<div id="email-input">' . $_POST['fes-email']; . '</div>
</form>
...';

请注意,在 $_POST 之前我已经删除了回声;如果您已经在回显,则没有理由开始回显。当您发布 ' . $_POST['fes-email'] 。 ' 你只是允许一个变量;它一直在运行回声,所以新回声所做的一切都是混淆它。如果你必须以这种方式拥有它,那么最好有一些类似于

的东西
  • htdocs(文件夹)
  • index.php(文件)
  • 包括(文件夹)
  • 包括/YourForm.php 然后在 YourForm.php 添加你想要的 HTML 然后在 index.php 添加

    require_once("includes/YourForm.php");

虽然我不同意你现在的做法,但我发布的内容应该可以。 不知道为什么要求没有格式化为代码。对不起。

【讨论】:

  • 我知道。我已经消除了回声。但仍然得到同样的错误。不知道为什么...如果您想看一下,我发布了完整的代码。
  • 为此我肯定会说使用包含。当我拿到我的电脑时,我会看得更深。还!发布确切的错误。它会准确地说明发生了什么。
【解决方案3】:

为什么要在 PHP 文件中回显 HTML。你可以做的是,你可以在你的条件中包含 HTML 文件。示例:-

if(condition 1) 
{
   include "one.php";
}
else if(condition 2)
{
  include "two.php";
}

在你的 one.php 和 two.php 中有你的 HTML,在你的情况下,你的所有 HTML 将转到这些文件之一。在该文件中,您可以回显您的变量

<div id="email-input"><?php echo $_POST['fes-email']; ?></div>

【讨论】:

  • 嗯,很高兴有人说服了他。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多