New to PHP .. problems with first college assignment

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.

The instructions are a bit, misguided, and I am not a fan of two different action for a single form without using javascript to handle it…

Personally, I would use sessions to store the values for when you redirect to the confirm page. And the index page would clean out any leftover values. Hidden fields that hold values, are not a good idea, in fact, that was an easy way to rip off original e-commerce stores; they would use hidden fields to store prices for the cart and were easily modified.

I know I late to the game. but I think the instructor is jumping the gun a little bit with this assignment. Though I can see what the instructor is trying to get at. The confirmation page normally would be saving the data to the database table or sending the data via the email server. Another way of doing this without saving the data in session might be to use some kind of token though it still uses sessions.

An Example
if (empty($_SESSION[‘token’])) {
$_SESSION[‘token’] = bin2hex(random_bytes(32));
}

Though doing something like this would create a lot more code and work, plus this is something that the instructor isn’t trying to get at. While it’s ugly and not particularly safe I would just create hidden fields in the form or generate gets in order to get the grade. I know when I took programming courses in college myself that sometimes I had to plug my nose and just do want the instructor wanted.

Thank you for your help. It was the first college assignment in PHP and had to be completed following specific instructions. I couldn’t use javascript or sessions (hadn’t even heard of sessions at that time). And the professor suggested I use hidden fields when I emailed him for help. I hear many negative comments about his teaching style, unfortunately. It makes it more of a challenge to find help from others. I’m sorry I didn’t respond sooner. I had issues trying to edit my first post here and in the process got a pop-up telling me that my account had been flagged and locked until administration could check it out. I wasn’t able to participate and had to seek help elsewhere. I’m back now with my second assignment. Completely lost, again.

I appreciate your input and apologize for not responding sooner. I had issues editing my first post (trying to add a picture of the assignment instructions) and during the process I received a pop-up telling me that my account was flagged and locked until administrative review. I couldn’t participate and had to seek help elsewhere (a disaster). I have a new assignment now and hoping to get some help. The professor just introduced the class to sessions. I’ll need to use it on the next assignment.

I don’t recall any mention of your account being locked and I am on regularly M-F?

It showed as “temporarily on hold” but it is fine now. I’m able to post, thankfully. You had mentioned in my current post that you may be available for tutoring. If so, I would certainly like to discuss this further. This is my last semester. My goal is web development but I’ve had to fight with this junior college to get the education that I was promised 18 months ago. I spend most of my time focusing on programming languages I never hope to use and that are certainly not required for local junior developer jobs. After 18 months my education has required that I build only 1 website and that was a year ago. I’ve considered leaving college to focus solely on html, css, and javascript … to at least gain a solid foundation to build on. But I’ve come this far. I need to just get through this PHP class and get my degree. I’ve maintained a 4.0 every semester but I’m at the point now that I no longer care about that. I don’t understand PHP and can’t seem to develop the logic needed to connect the dots, so to speak. I don’t even understand the answers given to me by those trying to help - terminology and concepts described that I have no knowledge of. I keep thinking of the scene in The Office when Michael says to Oscar, “Why don’t you explain this to me like I’m an 8 year old” ( sorry if you aren’t an Office fan lol) I’m giving you more info than necessary … but this is where I’m at right now and feeling a bit desperate for some help.

Sponsor our Newsletter | Privacy Policy | Terms of Service