2

Do I include the HTML code <table> in my PHP echo? or do I have to create a bunch of PHP lines in my HTML file? I'm new to PHP and I've never used tables before. I tried inputting the HTML code in my echo but I get errors because of the extra ""quotations that I used.

This is the result that I'm trying to get: 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
  • I'm not sure I quite get what you are asking, but you can always exit PHP to output raw HTML, like if ($int > 2) { ?><b>HTML here</b><?php } - you can also escape your quotes by using a backslash, like echo "\""; would output " Commented Jan 24, 2016 at 2:11

1 Answer 1

2

Yes you can add html code in php file like below:-

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

Note:-You can also use raw html in php file after ?> and still in your raw html again you can use php code. Note that this will happen only if file extension is .php not .html.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! Although I'm getting an error "Parse error: syntax error, unexpected 'Sub' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs...on line 29" Any idea what's wrong? I kept getting this same error earlier when trying to do it myself
echo '<table style="width:500px;border:2px solid green;">
Great to hear that. Glad to help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.