2017-01-12 6 views
0

私はいくつかのPHPコードを取得しようとしており、コードは本に従っています。しかし、このプログラムの割引を計算すると、計算はうまくいくが、名前は表示されません。これはすべてが正確に本の中でどのようになっているのかということで頭がおかしくなります...商品説明(名前)を表示させるにはどうすればよいですか?

2番目のセットこれで私を助ける目の?私はいくつかの画像と私のコードを提供します。

すべてが計算される前に、これは次のとおりです。 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> 

ポストの結果はどこにでも

フェッチされていない第二部の前の行$product_description = $_POST['product_description'];がなければならない:全体のコードすることはできません

<?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> 
+0

をあなたは変数 '$のPRODUCT_DESCRIPTIONを設定することはありません'何にでも。ユーザーがフォームを送信すると '$ _POST ['product_description']'になります。 – ceejayoz

+0

コードが途切れていることに気付きました。申し訳ありません。全体のコードを表示できるように自分の投稿を編集しました。 – HawkBlade124

答えて

1

を:それは計算された後のコードは、割引、すべてを表示しますproduct_description入力フィールドから変数に値を取得することができます。追加されたコードのIN QUESTION AFTER

ADDITION:

あなたはそこに追加の(間違った)スペースを得た:

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

はそれを削除します。

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

コードが途切れていることに気付きました。申し訳ありません。私はコード全体を表示するように編集しました。 – HawkBlade124

+0

私の答えに追加を参照してください – Johannes

+0

ありがとう、あなたの助けをありがとう!愚かなタイプミス。ハハ。 – HawkBlade124

関連する問題