<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Product Cost Calculator</title>
</head>
<body><!-- Script 4.1 - calculator.html -->
<div><p>Fill out this form to calculate the total cost:</p>
<?php
//test to see if submitted
    setlocale(LC_MONETARY, 'en_GB');

if ($_POST['submit'] == 'Calculate!' && $_SERVER['HTTP_REFERER'] == 'http://mm214.com/handle_calc.php')  {
    //referer is the value of what page sent the data
        //get the variables
    $price = $_POST['price'];
    $values = true;
    if (!is_numeric($price)){
       // echo '<script>alert("The price you entered is not a number");</script>';
        //above is example of javascript inside php
        //exit();<!-- bad! Removes form
        $values = false;
        echo ' You must have a number in the price field.';
    }
    
    $quantity = $_POST['quantity'];
    if (!is_numeric($quantity)){
       // echo '<script>alert("The price you entered is not a number");</script>';
        //above is example of javascript inside php
        //exit();<!-- bad! Removes form
        $values = false;
        echo  ' You must have a number in the quantity field.';
    }
     if (!is_numeric($discount)){
         
        //above is example of javascript inside php
        //exit();<!-- bad! Removes form
        $values = false;
        echo ' You must have a number in the discount field.';
    }
    
    $shipping = $_POST['shipping'];
    $tax = $_POST['tax'];
      if (!is_numeric($tax)){
       // echo '<script>alert("The price you entered is not a number");</script>';
        //above is example of javascript inside php
        //exit();<!-- bad! Removes form
        $values = false;
        echo  ' You must have a number in the tax field.';
    }
     
    $payments = $_POST['payments'];
      if (!is_numeric($payments)){
       // echo '<script>alert("The price you entered is not a number");</script>';
        //above is example of javascript inside php
        //exit();<!-- bad! Removes form
        $values = false;
        echo ' You must have a number in the payments field.';
    }

//multiply Price * Qty
    $total = $price * $quantity;
//subtract the discount
    $total = $total - $discount; 
//add the tax
    $total = ($total * $tax) + $total;  
//add the shipping
    $total = $total + $shipping; 
    $total = $total /$payments;
//divide by the number of payments
//handle errors     
    echo "Total: ".money_format('%i',$total); 
    
    
    
}  
//test to see if submitted
//get the variables 
//multiply Price * Qty
//subtract the discount
//add the tax
//add the shipping
//divide by the number of payments
//handle errors    
    
?>    
  
    
    <form action="handle_calc.php" method="post">

<p>Price: <input type="text" name="price" size="5" value="0"></p>

<p>Quantity: <input type="number" name="quantity" size="5" min="1" value="1"></p>

<p>Discount: <input type="text" name="discount" value="0" size="5"></p>

<p>Tax: <input type="text" name="tax" size="5" value="0.00"> (%)</p>

<p>Shipping method: <select name="shipping">
<option value="5.00">Slow and steady</option>
<option value="8.95">Put a move on it.</option>
<option value="19.36">I need it yesterday!</option>
</select></p>

<p>Number of payments to make: <input type="number" name="payments" size="5" min="1" value="1"></p>

<input type="submit" name="submit" value="Calculate!">

</form>

</div>
</body>
</html>