如何在HTML表格中显示PHP echo?

时间:2016-01-24 02:07:45

标签: php html echo

我是否在PHP回显中包含HTML代码<table>?或者我必须在我的HTML文件中创建一堆PHP行吗?我是PHP的新手,我之前从未使用过表。我尝试在回声中输入HTML代码但由于我使用了额外的“”引用而出现错误。

这是我想要获得的结果: enter image description here

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
            <title>Pizza Order Form</title>
            <link rel="stylesheet" type="text/css" href="pizza.css">
    </head>

<body>
<form method="post" name="frmPizzaOrder" action="serverCode/order.php">
    <div class="table">
        <div class="row">
            <div class="cell">Name:</div>   
            <div class="cell">
                <input type="text" name="txtName" id="txtName" autofocus>
                Phone:  <input type="text" name="txtPhone" id="txtPhone" placeholder="999-999-9999">
                <input type="submit" id="btnMessage" value="Get Total">
            </div>
        </div>


        <div class="row">
            <div class="cell">Base Pizza:</div>
            <div class="cell">
                <select id="cboBase" name="cboBase">
                    <option value="">Select a starter pizza ...</option>
                    <option value="10.25">Canadian Eh! -- $10.25</option>
                    <option value="12.00">Hawaiian -- $12.00</option>
                    <option value="12.50">Chili Dawg -- $12.50</option>
                    <option value="15.00">Meat Lover's Deluxe -- $15.00</option>           
                </select>
            </div>
        </div>
        <div class="row">
            <div class="cell">Pizza Size:</div>
            <div class="cell">
                <input type="radio" name="radSize" id="radSmall" value="0">Small
                <input type="radio" name="radSize" id="radMedium" value="5">Medium
                <input type="radio" name="radSize" id="radLarge" value="7">Large
            </div>

        </div>
        <div class="row">
            <div class="cell">Options:</div>
            <div class="cell">
                <input type="checkbox" name="chkOptions[]" id="chkExtraCheese" value=".5">Extra Cheese
                <input type="checkbox" name="chkOptions[]" id="chkExtraSauce" value=".25">Extra Sauce
                <input type="checkbox" name="chkOptions[]" id="chkOlives" value=".75">Olives
                <input type="checkbox" name="chkOptions[]" id="chkAnchovies" value=".75">Anchovies
            </div>
        </div>
    </div>
</form>

</body>
</html>

PHP:

<?php
    $name = $_POST["txtName"];
    $phone = $_POST["txtPhone"];
    $totalTop = 0;

    if (!$name || !isset($_POST['radSize']) || $_POST['cboBase'] == 0)
    {
        echo "Your name, base pizza and size selections are required.";
    }

    else {

        if (isset($_POST['chkOptions']))
        {
            $numOpt = count($_POST["chkOptions"]);

            foreach ($_POST["chkOptions"] as $tops)
            {
                $totalTop += $tops;
            }
        }

        $base = $_POST["cboBase"];
        $size = $_POST["radSize"];
        $subTotal = $base + $size;
        $total = $subTotal + $totalTop;

        echo "Thank-you for your order, $name. Here are your order details:<br>
        Sub Total: $subTotal<br>
        Number Of Options: ".$numOpt."<br>
        Options Total: $totalTop<br>
        Total: $total";
    }

?>

1 个答案:

答案 0 :(得分:1)

是的,您可以在php文件中添加html代码,如下所示: -

<?php
$name = $_POST["txtName"];
$phone = $_POST["txtPhone"];
$totalTop = 0;

if (!$name || !isset($_POST['radSize']) || $_POST['cboBase'] == 0)
{
    echo "Your name, base pizza and size selections are required.";
}else {
    if (isset($_POST['chkOptions']))
    {
        $numOpt = count($_POST["chkOptions"]);

        foreach ($_POST["chkOptions"] as $tops)
        {
            $totalTop += $tops;
        }
    }
    $base = $_POST["cboBase"];
    $size = $_POST["radSize"];
    $subTotal = $base + $size;
    $total = $subTotal + $totalTop;
    echo "Thank-you for your order, $name. Here are your order details:<br>";
    echo '<table style="width:500px;border:2px solid green;"><tr><td style="border:1px solid blue">Sub Total:</td><td style="border:1px solid blue">"'.$subTotal.'"</td></tr><tr><td style="border:1px solid blue">Number Of Options:</td><td style="border:1px solid blue">"'.$numOpt.'"</td></tr><tr><td style="border:1px solid blue">Options Total:</td><td style="border:1px solid blue">"'.$totalTop.'"</td></tr><tr><td style="border:1px solid blue">Total:</td><td style="border:1px solid blue">"'.$total.'"</td></tr></table>';
?>

注意: - 您也可以在?>之后在php文件中使用原始html,并且仍然在原始html中再次使用php代码。请注意,仅当文件扩展名为.php而不是.html时,才会发生这种情况。