PhP Form Dooley\'s Automotive Customer First Name: Customer Last Name: Labor Cos
ID: 3742150 • Letter: P
Question
PhP Form
Dooley's Automotive
Customer First Name:
Customer Last Name:
Labor Costs:
Parts Cost:
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.
here is the code i have so far need to calculate the php in the form any help would be great
<?php
//get the data from the form and filter input
//validate user input (not empty, not FALSE, and greater than zero)
//if loop to call index.php when error message is not empty
//do the math (use the define function to create a constant called TAX_RATE)
//format variables as strings for display as currency and concatenate
//first and last names into one customer name
?>
<!DOCTYPE html>
<html>
<head>
<title>Invoice Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<main>
<h1>Dooley's Automotive</h1>
<label>Customer's Name:</label>
<span><?php echo 'statement goes here'; ?></span><br>
<label>Labor Costs:</label>
<span><?php echo 'statement goes here'; ?></span><br>
<label>Parts Cost:</label>
<span><?php echo 'statement goes here'; ?></span><br>
<label>Subtotal:</label>
<span><?php echo 'statement goes here'; ?></span><br>
<label>Sales Tax:</label>
<span><?php echo 'statement goes here'; ?></span><br>
<label>Total:</label>
<span><?php echo 'statement goes here'; ?></span><br>
</main>
</body>
</html>
Explanation / Answer
<?php
//This is index.php file
?>
<!DOCTYPE html>
<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="taxcalculate.php" method="POST">
<label>Customer's First Name:</label>
<span> <input type="text" name="CustomerFName"></span> <br>
<label>Customer's Last Name</label>
<span> <input type="text" name="LastName"> </span><br>
<label>Labor Costs:</label>
<span> <input type="text" name="LaborCost"/></span><br>
<label>Parts Cost:</label>
<span> <input type="text" name="PartsCost"/></span><br>
<input type="submit" value="Calculate">
</form>
</main>
</body>
</html>
<?php
//This is taxcalculate.php file
?>
<!DOCTYPE html>
<html>
<head>
<title>Invoice Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<main>
<?php
$FirstName= filter_input(INPUT_POST, 'CustomerFName', FILTER_SANITIZE_STRING);;
$LastName= filter_input(INPUT_POST, 'LastName', FILTER_SANITIZE_STRING);;
$LabourCost= filter_input(INPUT_POST, 'LaborCost', FILTER_SANITIZE_STRING);
$PartsCost= filter_input(INPUT_POST, 'PartsCost', FILTER_SANITIZE_STRING);
$cust_name=$FirstName.' '.$LastName;
$subTotal=0.0;
$SalesTax=0.0;
$Total=0.0;
if($FirstName != "" and is_string($FirstName)){
if($LastName != "" and is_string($LastName)){
if($LabourCost > 0.0 and is_float((float)$LabourCost)){
if($PartsCost > 0.0 and is_float((float)$PartsCost)){
define("TAX_RATE",".0925");
$subTotal= (float)$LabourCost + (float)$PartsCost;
$SalesTax= (float)TAX_RATE * $subTotal;
$Total= $SalesTax + $subTotal;
}
else
{
echo "You have not entered Parts Cost or You have entered wrong value";
header("Location: /index.php");
exit();
}
}
else
{
echo "You have not entered Labour Cost or You have entered wrong value";
header("Location: /index.php");
exit();
}
}
else
{
echo "You have not entered Last name";
header("Location: /index.php");
exit();
}
}
else
{
echo "You have not entered First name";
header("Location: /index.php");
exit();
}
?>
<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('$'.$LabourCost); ?>"/></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>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.