首页 > 解决方案 > PHP 输出 HTML,更好的方法?

问题描述

基本上这就是我所拥有的:

<form method="POST" action="model.php">
            <input type="hidden" name="from" value="edit">
            <?php
                include 'model.php';
                $contactID = $_POST['editConRad'];
                selectSingle($contactID);
                echo "<input type='hidden' name='contactID' value='$contactID'>";
                echo "<label>First Name:</label>";
                echo "<input type="text" name="fname">";
                echo "<label>Last Name:</label>";
                echo "<input type="text" name="lname">";
                echo "<label>Email:</label>";
                echo "<input type="email" name="email">";
                echo "<label>Street Address:</label>";
                echo "<input type="text" name="streetaddress">";
                echo "<label>City:</label>";
                echo "<input type="text" name="city">";
                echo "<label>Province:</label>";
                echo "<input type="text" name="province">";
                echo "<label>Postal Code:</label>";
                echo "<input type="text" name="postalcode">";
                echo "<label>Phone:</label>";
                echo "<input type="number" name="phone">";
                echo "<label>Date of Birth:</label>";
                echo "<input type="date" name="yob">";
                echo "<input type="submit" value="Submit Edit">";
            ?>
        </form>

而且我认为必须有更好的方法来输出 html 而不是必须用 echo 输出每一行,我认为有一个打印功能可以做到这一点,但我找不到。

有人有更好的方法吗?我知道我可以做一个巨大的回声,但肯定还有更好的东西。

我想这种方式比我上面的方式更好,但仍然没有寻找更好的方式。

                echo '<label>First Name:</label>.
                <input type="text" name="fname">.
                <label>Last Name:</label>.
                <input type="text" name="lname">.
                <label>Email:</label>.
                <input type="email" name="email">.
                <label>Street Address:</label>.
                <input type="text" name="streetaddress">.
                <label>City:</label>.
                <input type="text" name="city">.
                <label>Province:</label>.
                <input type="text" name="province">.
                <label>Postal Code:</label>.
                <input type="text" name="postalcode">.
                <label>Phone:</label>.
                <input type="number" name="phone">.
                <label>Date of Birth:</label>.
                <input type="date" name="yob">.
                <input type="submit" value="Submit Edit">';

标签: phphtml

解决方案


只要添加适当的标签,您就可以随时在 PHP 和 HTML 之间切换。

例如:

<?php
echo "This is output from an echo statement<br>"
?>
This is plain old HTML<br>
<?php
echo "And now we're back to PHP<br>"

你的表格变成:

<form method="POST" action="model.php">
        <input type="hidden" name="from" value="edit">
        <?php
            // Do this bit in PHP
            include 'model.php';
            $contactID = $_POST['editConRad'];
            selectSingle($contactID);
        ?>
            <!-- Now switch back to HTML -->
            <!-- PHP variable embedded here-------------|................| -->
            <input type='hidden' name='contactID' value='<?= $contactID?>'>";
            <label>First Name:</label>
            <input type="text" name="fname">
            <label>Last Name:</label>";
            <input type="text" name="lname">
            <label>Email:</label>
            <input type="email" name="email">

            <!-- and so on -->
         
    </form>

<?php
// You can store long text strings in HEREDOC or NOWDOC syntax
$longString = <<<'EOT'
    This is an arbitrarily long string, but this one is only short<br>
EOT
echo $longString;

有关 PHP如何处理字符串、HEREDOC、NOWDOC 以及单引号和双引号的重要性的所有详细信息,请参阅PHP 手册。


推荐阅读