Web Design/Generating a receipt with PHP
Once your basic response and email are all happening, and you're feeling a bit more confident with your first PHP pages, it's a good time to start learning a bit more about PHP variables and control structures! But before moving on from here, please make sure that your response.php currently:
- Responds personally to the user
- Includes the details of the product selected by the user (its name and its price)
- includes the billing information for the user.
Web Design → Generating a receipt with PHP
|
Adding up the total
editIf a visitor to your site indicates that they want to buy 5 T-shirts at $15 each, we need to let them know what their total comes to! To do this, we'll need to create a variable.
You might have noticed that variables in PHP always have a dollar sign ($) in front of them. Here are a few examples:
$num1 = 12; // This is a number $string1 = "Here's a string in double quotes"; $string2 = 'Here\'s a string in single quotes';
Can you guess why the apostrophe (') in $string2 has a back-slash (\) in front of it?
We need to create a new variable called $total in our response.php. Initially we'll set it to 0 like this:
$total = 0; // We'll use this variable to calculate the total for the order
but your challenge is to print out the total for the order when your form is submitted!
If your visitor submits your form indicating that they want to buy 5 T-shirts at $15 each, what would the total be? Fill in the blanks in the following:
$total = ___ * ___ ;
In our situation, we don't know how many items our visitors would like to buy until they submit the form. See if you can modify the code above to use your $quantity variable instead of '5'. Add this line to your code and print it out on your receipt.
No peaking at the solution until you have had a good attempt yourself!
You should already have a variable called $quantity in your response.php that takes the value from your form. For example, if your form includes the input field:
Please enter the number of T-shirts that you would like to buy: <input type="text" id="quantity" name="quantity" />
then your response.php would create the variable $quantity like this:
$quantity = $_POST["quantity"];
Let's pretend that whatever product of service that your form is selling costs $15 per item, then calculating the total and printing it out can be done as follows:
$total = $quantity * 15; print("<p>Total: $quantity items at 15 each comes to: $total</p>");
Extra: So you don't need to change your code everywhere when you put the price up to $20, create another variable $tshirt_price (or something relevant to your own form), set it equal to 15 and then use this variable in your calculation instead of the constant number 15.
Including postage in the total
editNow that we've calculated the total for our order, we need to include a postage fee! The following exercise will help you along the way!
To include the postage fee, we need to take our current total and add $20 to it, and then inform our customer of the total including postage.
You'll need to add the following code (after filling in the blanks) to calculate the new total:
$total = ________ + ________; // Add the postage fee of 20 to our current total
and then include a new print() statement that informs your visitor of the total including postage!
Sorry - hope to have time soon. When you get the answer, how about adding it in here!
Free delivery for bulk buys
editLet's say you decide it would be good for business if you offer free delivery if customers buy more than 6 items.
Your pseudo-code should look something like this:
if the quantity is less than 7 then do the following: add the $20 postage fee to the total
You might want to take a look at if-statements in the PHP manual or Free2Code's explanation of if-statements to get an idea of how if-statements work in PHP (they're nearly identical to if-statements in JavaScript).
TO DO: Exercise with solution
Discount if you spend over $200
editFinally, if your customers spend over $200 (modify to suit your needs), they get a 10% discount.
Again your pseudo-code should look like...
$discountmin = 200; if the quantity is more than discountmin then: multiply amount by 0.9 //0.9 basically just takes away the 10%