Reputation: 107
so I am trying to get some PHP code working and the code is according to the book. But, when I calculate the discount on this program, the calculations are fine but the name doesn't appear.This is mind boggling because everything is exactly how it is in the book...
Can I get a second set of eyes to help me out on this? I'll provide some images and my code.
This is before everything is calculated:
This is after everything is calculated ("Guitar" is supposed to appear next to product description):
This code is the HTML template before everything is calculated:
<!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> </label>
<input type="submit" value="Calculate Discount"><br>
</form>
</main>
</body>
</html>
This code displays the discount and everything after it has been calculated:
<?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>
Upvotes: 0
Views: 301
Reputation: 67778
that can't be the whole code: The post results are not fetched anywhere
There has to be a line $product_description = $_POST['product_description'];
before the second part somewhere, to get the value from the product_description input field into the variable.
ADDITION AFTER ADDED CODE IN QUESTION:
You got an additional (wrong) space in there:
$product_description = filter_input(INPUT_POST, 'product _description');
remove it:
$product_description = filter_input(INPUT_POST, 'product_description');
Upvotes: 1