如何显示产品描述(名称)?

时间:2017-01-12 23:14:27

标签: php

所以我试图让一些PHP代码工作,代码是根据书中的。但是,当我计算这个程序的折扣时,计算很好,但名字没有出现。这是令人难以置信的,因为一切都与书中的内容完全相同......

我能否得到第二眼帮助我解决这个问题?我会提供一些图片和我的代码。

这是在计算所有内容之前: This is before everything is calculated

这是在计算完所有内容之后(“吉他”应该出现在产品说明旁边):

This is after everything is calculated

此代码是计算所有内容之前的HTML模板:

<!DOCTYPE html>
<html>

<head>
    <title> Product Discount Calculator</title>
    <link rel="stylesheet" type="text/css" href="main.css">
</head>

<body>
    <main>
        <h1>Product Discount Calculator (Complete)</h1>
        <form action="display_discount.php" method="post">
            <div id="data">
                <label>Product Description</label>
                <input type="text" name="product_description"><br>

                <label>List Price</label>
                <input type="text" name="list_price"><br>

                <label>Discount Percent:</label>
                <input type="text" name="discount_percent"><span>%</span><br>
            </div>

            <div id="buttons">
                <label>&nbsp;</label>
                <input type="submit" value="Calculate Discount"><br>
        </form>
    </main>
</body>
</html>

此代码显示折扣及其计算后的所有内容:

<?php
    //get data from the form
    $product_description = filter_input(INPUT_POST, 'product _description');
    $list_price = filter_input(INPUT_POST, 'list_price');
    $discount_percent = filter_input(INPUT_POST, 'discount_percent');

    //Calculate the discount 
    $discount = $list_price * $discount_percent * .01;
    $discount_price = $list_price - $discount;

    //apply currency formatting to the dollar and percent amounts
    $list_price_f = "$".number_format($list_price, 2);
    $discount_percent_f = $discount_percent."%";
    $discount_f = "$".number_format($discount, 2); 
    $discount_price_f = "$".number_format($discount_price, 2);

?>

<!DOCTYPE html>
<html>

<head>
    <title> Product Discount Calculator (Complete)</title>
    <link rel="stylesheet" type="text/css" href="main.css">
</head>

<body>
    <main>
        <h1> Product Discount Calculator</h1>
                <label>Product Description:</label>
                <span><?php echo htmlspecialchars($product_description); ?></span>
                <br>

                <label>List Price:</label>
                <span><?php echo htmlspecialchars($list_price_f); ?></span><br>

                <label>Standard Discount:</label>
                <span><?php echo htmlspecialchars($discount_percent_f); ?></span><br>

                <label>Discount Amount:</label>
               <span><?php echo $discount_f; ?></span><br>

                <label>Discount Price:</label>
                <span><?php echo $discount_price_f; ?></span><br>
    </main>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

不能成为整个代码:不会在任何地方提取帖子结果

在第二部分之前必须有一行$product_description = $_POST['product_description'];,以便将product_description输入字段中的值输入变量。

在问题中添加代码之后的补充:

你有一个额外的(错误的)空间:

$product_description = filter_input(INPUT_POST, 'product _description');

将其删除:

$product_description = filter_input(INPUT_POST, 'product_description');