Hello all. I’m back in college at the age of 48. I’m taking a PHP course this semester and I’m stuck on my first assignment. I hope I can explain my issue in an appropriate way. I’m not adept at using the right technical terms.
The page I’ve coded has a “calculate button” that displays the calculated numbers on that page. Not a problem there. My issues is with the second page. The first page also a “confirm” button that takes the user to the second page where some of the information on the first page is displayed. My “confirm” button works but the calculated information does not transfer over. I’ve spent many hours just trying to correct this issue. I’ve googled to no avail. I emailed my professor who was no help … telling me to manually enter the 2nd page info as “hidden values”. I don’t know if I need to create 2 forms and 2 actions (one for the “calculate” button and one for the “confirm” button). He said to make both of the pages “postback” …but if the “calculate” button only does calculations on the first page, why do I need it to “postback” to it’s own page? And the “hidden values” ? We have yet to do this in class. I’ve tried examples I’ve found online but they are not working. I’m obviously doing something wrong. How would I go about getting help to get through this? Thank you
I’m editing to add the assignment and my code attempt
I’m sorry. I’m having trouble including a photo of the assignment that is large enough to read.
my index.php page :
<?php
// get the data from the form
$sales_price = filter_input(INPUT_POST,‘sales_price’, FILTER_VALIDATE_FLOAT);
$discount_percent = filter_input(INPUT_POST,‘discount_percent’,FILTER_VALIDATE_FLOAT);
$total_price = filter_input(INPUT_POST,‘total_price’, FILTER_VALIDATE_FLOAT);
// validate sales_price
if ($sales_price === FALSE) {
$sales_priceError = ‘Sales price must be a valid amount’;
} else if ($sales_price < 1.0) {
$sales_priceError = ‘Sales price must be greater than 0’;
} else {
$sales_priceError = ‘’;
}
// validate discount_percent
if ($discount_percent === FALSE) {
$discount_percentError = ‘Discount percent must be a valid amount’;
} else if ($discount_percent < 1.0) {
$discount_percentError = ‘Discount percent must be greater than 0’;
} else {
$discount_percentError = ‘’;
}
// calculate the discount and the discounted price
$discount_amount = $sales_price * $discount_percent / 100;
$total_price = $sales_price - $discount_amount;
?>
<!doctype html>
<html lang=“en”>
<head>
<title>Quote</title>
</head>
<body>
<section>
<h1>Price quotation</h1>
<form id=“priceForm” name=“priceForm” method=“post” action=’’>
<label for=“sales_price”>Sales price </label>
<input type=“text” id=“sales_price” name=“sales_price” required
value="<?php echo $sales_price; ?>" />
<?php if (!empty($sales_priceError)) : ?>
<span style=“color:red;background-color: white”>
<?php echo $sales_priceError; ?>
</span>
<?php endif; ?>
<br />
<br />
<label for=“discount_percent”>Discount percent </label>
<input type=“text” id=“discount_percent” name=“discount_percent” required
value="<?php echo $discount_percent; ?>" />
<?php if (!empty($discount_percentError)) : ?>
<span style=“color:red;background-color: white”>
<?php echo $discount_percentError; ?>
</span>
<?php endif; ?>
<p>Discount amount <?php echo ‘$’.number_format($discount_amount,2);?></p>
<p>Total price <?php echo ‘$’.number_format($total_price, 2);?></p>
<input type=“submit” name=“submitButton” id=“submitButton” value=“Calculate” />
</form>
<form id=“confirmForm” name=“confirmForm” method=“post” action=“confirm.php”>
<input type=“hidden” id=“sales_price” name=“sales_price”/>
<input type=“hidden” id=“discount_percent” name=“discount_percent”/>
<input type=“hidden” id=“discount_amount” name=“discount_amount”/>
<input type=“hidden” id=“total_price” name=“total_price”/>
<input type=“submit” name=“confirmSubmit” id=“confirmSubmit” value=“Confirm” />
</form>
<p>Enter price and discount amount and click Calculate</p>
</section>
</body>
</html>
and my confirm.php page ….
<?php
$sales_price = filter_input(INPUT_POST,‘sales_price’,FILTER_VALIDATE_FLOAT);
$discount_amount = filter_input(INPUT_POST, ‘discount_amount’,FILTER_VALIDATE_FLOAT);
$discount_percent = filter_input(INPUT_POST, ‘discount_percent’,FILTER_VALIDATE_FLOAT);
$total_price = filter_input(INPUT_POST, ‘total_price’, FILTER_VALIDATE_FLOAT);
$name = filter_input(INPUT_POST,‘name’);
$email = filter_input(INPUT_POST,‘email’,FILTER_VALIDATE_EMAIL);
// validate name
if ($name === ‘’) {
$nameError = ‘Name is required’;
} else {
$nameError = ‘’;
}
?>
<!doctype html>
<html lang=“en”>
<head>
<title>Confirmation</title>
</head>
<body>
<section>
<h1>Quotation confirmation</h1>
<p>Sales price $<?php echo number_format($sales_price, 2); ?></p>
<p>Discount amount $<?php echo number_format($discount_amount, 2); ?></p>
<p>Total price $<?php echo number_format($total_price, 2); ?> </p>
<h2> Send confirmation to</h2>
<form id=“nameForm” name=“nameForm” method=“post”>
<label for=“name”>Name </label>
<input type=“text” id=“name” name=“name” required
value="<?php echo $name; ?>" />
<?php if (!empty($nameError)) : ?>
<span style=“color:red;background-color: white”>
<?php echo $nameError; ?>
</span>
<?php endif; ?>
<br />
<br />
<label for=“email”>Email address</label>
<input type=“email” id=“email” name=“email” required />
</form>
<br />
<input type=“submit” name=“quoteSubmit” id=“quoteSubmit” value=“Send Quotation” />
<form id=“returnForm” name=“returnForm” action=“index.php”>
<input type=“Submit” name=“returnSubmit” id=“returnSubmit” value=“Return” />
</form>
</section>
</body>
</html>
Here is the response I received from my professor about my issue:
Part of this assignment is finding the way to make the two pages to work together. You can approach it in many ways. I would consider making the validation and calculations happen in a postback and the confirmation will postback then send the user to the other page. Consider the two examples we did last week as examples of making the pages work together. When you transfer the data to the other page you will have to manually create the get or add the values as hidden values in the form.