Your question needs more information to be answered. Review expert comments belo
ID: 3744875 • Letter: Y
Question
Your question needs more information to be answered.
Review expert comments below. Then, edit your question.
Question: So i have half of this php calculation i need some help with i have the index.php part completed ...
Edit question
So i have half of this php calculation i need some help with i have the index.php part completed :
Here is the form :
where i create a php code block at the beginning of this document with if statements that use the !isset() built-in function to set the default value of the variables ($cust_fName, $cust_lName, $labor_costs, $parts_cost) to an empty string (‘ ‘) if the variables do not exist, specifically the first time the page loads. Just inside of the body, you will need to add another php statement with an if loop that uses the !empty() function to determine if the $error_message is not empty in which case it will echo the contents of the $error_message variable. Use echo statements within the value attributes of the tags for the text boxes. These echo statements should display the values in the variables from the php code block at the beginning of the page. Be sure to use the htmlspecialchars() function in your echo statements to escape the output before it is sent to the browser. After adding php blocks to your html document it is necessary to save it as a php file type.
my Code for index.php
<?php
//set default value of variables for initial page load
if (!isset($investment)) { $investment = ''; }
if (!isset($interest_rate)) { $interest_rate = ''; }
if (!isset($years)) { $years = ''; }
?>
<!DOCTYPE html>
<html>
<head>
<title>Future Value Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<main>
<h1>Future Value Calculator</h1>
<?php if (!empty($error_message)) { ?>
<p class="error"><?php echo htmlspecialchars($error_message); ?></p>
<?php } ?>
<form action="display_results.php" method="post">
<div id="data">
<label>Investment Amount:</label>
<input type="text" name="investment"
value="<?php echo htmlspecialchars($investment); ?>">
<br>
<label>Yearly Interest Rate:</label>
<input type="text" name="interest_rate"
value="<?php echo htmlspecialchars($interest_rate); ?>">
<br>
<label>Number of Years:</label>
<input type="text" name="years"
value="<?php echo htmlspecialchars($years); ?>">
<br>
</div>
<div id="buttons">
<label> </label>
<input type="submit" value="Calculate"><br>
</div>
</form>
</main>
</body>
</html>
Now i need some help with the display_invoice.php as follows :
The php block at the beginning of the document will contain five parts. The first part gets the data from the form by using the filter_input() function to access the POST request. The second part uses one long if/else if statement to validate all four user entries. All four fields are required. $labor_costs and $parts_cost must be valid floats and they must be greater than zero. Don’t forget to include a final else statement that sets $error_message equal to an empty string. The third section is an if statement that reloads the index.php page if an error message was generated in the second section. The next section is where the math takes place. Use the define() function to create a constant TAX_RATE equal to .0925 or 9.25%. You will need variables called: $subTotal, $total, $sales_tax. The final section of the php block formats the display so that all amounts print in currency format, and concatenate the $cust_fName and $custlName with a space between them into a variable called $f_cust_name. In the body of the document use echo statements to display the customer name, parts cost, labor costs, subtotal, sales tax, and total within the <span> tags. so my calculations are wrong can you take a look and see where im going wrong
<?php
$cust_fName = filter_input(INPUT_POST, 'cust_fName',
FILTER_VALIDATE_FLOAT);
$cust_lName = filter_input(INPUT_POST, 'cust_lName',
FILTER_VALIDATE_FLOAT);
$labor_cost = filter_input(INPUT_POST, 'labor_costs',
FILTER_VALIDATE_INT);
$parts_costs = filter_input(INPUT_POST, 'parts_costs',
FILTER_VALIDATE_INT);
// validate Name
if ($cust_fName === FALSE ) {
$error_message = 'Must enter a First Name.';
} else if ( $cust_lName === FALSE ) {
$error_message = 'Must Enter a last name .';
// validate labor Cost
} else if ( $labor_cost === FALSE ) {
$error_message = 'labor cost must be a valid number.';
} else if ( $labor_cost <= 0 ) {
$error_message = 'labor cost must be greater than zero.';
// validate parts cost
} else if ( $parts_costs === FALSE ) {
$error_message = 'parts costs must be a valid number.';
} else if ( $parts_costs <= 0 ) {
$error_message = 'parts costs must be greater than zero.';
// set error message to empty string if no invalid entries
} else {
$error_message = '';
}
// if an error message exists, go to the index page
if ($error_message != '') {
include('index.php');
exit(); }
// calculate
$subtotal = labor_cost + parts_costs;
for ($i = 1; $i <= $subtotal; $i++) {
$Total =
$labor_cost + ($parts_costs * $salesTax * .01);
}
// apply currency and percent formatting
$subtotal = '$'.number_format($subTotal, 2);
$salesTax = $interest_rate.'%';
$Total= '$'.number_format($Total, 2);
?>
<!DOCTYPE>
<html>
<head>
<title>Invoice Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<main>
<h1>Dooley's Automotive</h1>
<form action="index.php" method="POST">
<label>Customer's Name:</label>
<span> <input readonly type="text" name="CustomerFName" value="<?php echo htmlentities($cust_name); ?>"/></span> <br>
<label>Labor Costs:</label>
<span> <input readonly type="text" name="LaborCost" value="<?php echo htmlentities('$'.$LaborCost); ?>"/></span><br>
<label>Parts Cost:</label>
<span> <input readonly type="text" name="PartsCost" value="<?php echo htmlentities('$'.$PartsCost); ?>"/></span><br>
<label>Subtotal:</label>
<span><input readonly type="text" name="subtotal" value="<?php echo htmlentities('$'.$subTotal); ?>"/></span><br>
<label>Sales Tax:</label>
<span><input readonly type="text" name="salesTax" value="<?php echo htmlentities('$'.$SalesTax); ?>"/></span><br>
<label>Total:</label>
<span><input readonly type="text" name="salesTax" value="<?php echo htmlentities('$'.$Total); ?>"/></span><br>
<input type="submit" value="Goto Main Page">
</form>
</main>
</body>
Dooley's Automotive Customer First Name: Customer Last Name: Labor Costs Parts Cost: Display InvoiceExplanation / Answer
I have seen the code, is this calculation correct as per you web development..
It is better to use different variable names for $Total as you are using twice.
//Calculate Method.
$subtotal = labor_cost + parts_costs;
for ($i = 1; $i <= $subtotal; $i++) {
$Total = $labor_cost + ($parts_costs * $salesTax * .01);
}
// apply currency and percent formatting
$subtotal = '$'.number_format($subTotal, 2);
$salesTax = $interest_rate.'%';
$Total= '$'.number_format($Total, 2);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.