dtdr84101 2015-01-23 01:28
浏览 49
已采纳

php echo整个html而不使用readfile

As the tittle, is it possible to echo entire html and css? I am using if's and each match needs to generate a different page.

Also, in the html code in the html code I need to echo variable that is enclosed inside a div and styled.

Any thoughts how I can achieve this?

I get the following error syntax error, unexpected T_STRING, expecting

Thanks

    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>';
  • 写回答

3条回答 默认 最新

  • doudilin1225 2015-01-23 01:53
    关注

    I would recommend you to not print html stuff with php, just close the php tags and write normal html, so here i removed the big echo statement and wrote normal html. This makes the code more clean and you see better what is html and where is PHP.

    So your php code:

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

    If you don't have it in a echo statement just use this:

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

    This is pretty much the same as:

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

    Also for useful error messages you can turn on error reporting with this lines:

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

    You can just put them on top of your php files and make sure that they are only turned on in staging!

    The entire code should look something like this:

    <!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>
    

    EDIT:

    If you have this echo statement in a if statement you still don't need to print it that way :D

    As a example (Pseudo Code):

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

    Also if you have to concatenate stuff:

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

    And for more information about concatenation see the manual: http://php.net/manual/en/language.operators.string.php

    I would also recommend you to not write CSS inline or in style tags. You can write your own CSS files with *.css and then include them in the html head tag with this:

    <link rel="stylesheet" type="text/css" href="yourCssFile.css">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题