noobCoder
noobCoder

Reputation: 211

PHP out putting HTML, better way?

Basically this is what I have:

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

And I think there must be a better way to output html instead of having to output each line with echo, I thought there was a print function to do this but I couldnt find one.

Anyone have a better way of doing this? I know I could do one massive echo but there must still be something better.

I guess this way is better than what I have above but still not looking for a better way.

                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">';

Upvotes: 0

Views: 35

Answers (1)

You can switch between PHP and HTML whenever you wish just by adding the appropriate tags.

For example:

<?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>"

Your form becomes:

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

See the PHP manual for all the details on how PHP handles strings, HEREDOC, NOWDOC, and the significance of single and double quotes.

Upvotes: 1

Related Questions