Jump to content
Larry Ullman's Book Forums

I'm Having Some Trouble With My Calculations.


Recommended Posts

I first want to say that this book is awesome. I bought PHP for the Web the fifth edition. I have made two functional forms, and I'm so happy with the book. The version of PHP that I have installed is 7.1.10, and I'm on a Windows 64bit. 

Here is my HTML below.

<!doctype html>
  <html>
    <head>
	  <meta charset = "utf-8">
	  <title>Contact Us</title>
	  <meta name="viewport" content="width=device-width, initial-scale=1">
	  <style>
      label,
      #submit {
      	display: block;
      }
	  </style>
	</head>
	<body>
		<h1>The Ticket Center</h1>
	  <form action="wwf_tickets.php" method="post">
		  <label for="price">Price:</label>
		  <input type="text" id="price" name="price">

		  <label for="quantity">Quantity:</label>
		  <input type="number" id="quantity" name="quantity" min="1">

		  <label for="shipping">Shipping:</label>
		  <select id="shipping" name="shipping">
		  	<option value="5.00">Standard Delivery $5.00</option>
		  	<option value="10.00">Two Day $10.00</option>
		  	<option value="25.00">Overnight $25.00</option>
		  </select>

			<label for="tax">Tax:</label>
			<input type="number" id="tax" name="tax">

			<input type="submit" name="submit" value="Submit" id="submit">
	  </form>
	</body>
  </html>

Now the problem I'm having is, I want to show the cost of the tickets with tax (pre-grand total). For example, if I purchase 1 ticket at $45.00  with 10.00 shipping with a tax rate of .06, the tax would be $3.30, and I would like to print that result to the screen. For some reason, everything I tried I always get the grand total for everything. I didn't use the format_number() function yet because I want to figure out the current problem first. Here is my PHP below, can someone here please show me how to do that, and thank you in advance for any help.

<!doctype html>
  <html>
    <head>
	  <meta charset = "utf-8">
	  <title>Contact Us</title>
	  <meta name="viewport" content="width=device-width, initial-scale=1">
	</head>
	<body>
		<?php
		  // Display errors
      ini_set('display_errors', 1);
      error_reporting(E_ALL);

      $price = $_POST['price']; // The price for ticket/s
      $quantity = $_POST['quantity']; // Number of tickets wanting
      $shipping = $_POST['shipping']; // choosing a shipping option
      $tax = $_POST['tax']; // Entering the tax rate.

      $taxrate = $tax / 100; // getting the tax rate 
      $taxrate++; // adding 1 to the tax rate

      $total = ($price * $quantity) + $shipping; // price of the ticket times the quantity, and adding the shipping chosen
      $grand_total = $total * $taxrate; // getting the grand total  for the tickets, and getting the total cost with tax.

      print "<p>The ticket price you selected is: <span>$</span>$price</p>
      			 <p>The number of tickets you have selected is: $quantity</p>
             <p>The shipping you have selected is: <span>$</span>$shipping</p>
             <p>The tax rate you have selected is: $tax <span>%</span><br>
             <p>The cost of the ticket(s) including shipping is: <span>$</span>$total</p><br>
             <p>The total ticket(s) price including tax is: <span>$</span>$grand_total</p>
	  ?>
	</body>
  </html>
Edited by mike316
Link to comment
Share on other sites

If you want to print the tax (in dollars) you need to calculate the values separately (i.e. you can't simply add 1 to the tax rate)

$taxrate = $tax / 100; 
$total = ($price * $quantity) + $shipping;
$taxvalue = $total * $taxrate; 
$grandtotal = $total + $taxvalue;
  • Upvote 1
Link to comment
Share on other sites

 

If you want to print the tax (in dollars) you need to calculate the values separately (i.e. you can't simply add 1 to the tax rate)

$taxrate = $tax / 100; 
$total = ($price * $quantity) + $shipping;
$taxvalue = $total * $taxrate; 
$grandtotal = $total + $taxvalue;

Worked like a charm. Great job Iromao, and thanks. I spent some time trying to figure this out.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...