dtdr84101
dtdr84101
2015-01-23 01:28

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 doudilin1225 6年前

    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">
    
    点赞 评论 复制链接分享
  • dozr162106 dozr162106 6年前

    Why do you echo HTML in a PHP file . what you can do is , you can include HTML files in your condition . example :-

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

    in your one.php and two.php you have your HTML , in your case your all HTML will go to one of these files . in that file you can echo your variable

    <div id="email-input"><?php echo $_POST['fes-email']; ?></div>
    
    点赞 评论 复制链接分享
  • dongqing4070 dongqing4070 6年前
    echo '...assuming the previous part of the entire code are also present...
    <form>    
    <div id="email-input">' . $_POST['fes-email']; . '</div>
    </form>
    ...';
    

    Take notice that before $_POST I've removed the echo; If you're already echoing there's no reason to start the echo. When you post ' . $_POST['fes-email'] . ' You're just allowing a variable; it's been running the echo the whole time, so all the new echo does is confuse it. If you have to have it this way then it might be better to have something along the lines of

    • htdocs(folder)
    • index.php(file)
    • includes(folder)
    • includes/YourForm.php Then within YourForm.php add the HTML that you want to have then within index.php add

      require_once("includes/YourForm.php");

    While I don't agree with the way you're doing it right now what I posted should work. Not sure why the require isn't formatting as code.. Sorry.

    点赞 评论 复制链接分享

相关推荐