Page 1 of 1

Code producing error message and I can't figure out why

Posted: Mon Jun 14, 2010 1:56 pm
by canadian_angel

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
        <title>Product Cost</title>
    </head>
    <body>
        <?php // Script 4.5 - handle_calc.php

        /*This script takes values from calculator.html and performs
         total cost and monthly payment calculations. */

        // Address error handling.
        ini_set ('display_errors', 1);
        error_reporting(E_ALL & ~E_NOTICE);

        // In case register_globals is enabled.
        $price = $_POST['price'];
        $quantity = $_POST['quantity'];
        $discount = $_POST['discount'];
        $tax = $_POST['tax'];
        $shipping = $_POST['shipping'];
        $payments = $_POST['payments'];
        
        // Calculate the total
        $total=(($price * $quantity) + $shipping) - $discount;
        
        // Determine the tax rate
        $taxrate = $tax/100;
        $taxrate++;

       // Factor in the tax rate.
        $total = $total * $taxrate;

       // Calculate the monthly payments.
        $monthly = $total / $payments;

        // Apply the proper formatting.
        $total = number_format ($total, 2);
        $monthly = number_format ($monthly, 2);

       // Print out the results.
        print "You have selected to purchase:<br />
        <b>$quantity</b> widget(s) at <br />
        $<b>$price</b> price with a <br />
        $<b>$shipping</b> shipping cost and a <br />
        <b>$tax</b> percent tax rate. <br />
        After your $<b>$discount</b> discount, the total cost is
        $<b>$total</b>.<br />
        Divided over <b>$payments</b> monthly payments, that would be $<b>$monthly</b> each.";

        ?>

    </body>
</html>

Re: Code producing error message and I can't figure out why

Posted: Mon Jun 14, 2010 2:14 pm
by Benjamin
$_POST['payments'] must be a number greater than 0.

Re: Code producing error message and I can't figure out why

Posted: Mon Jun 14, 2010 3:46 pm
by canadian_angel
Ok so srry does that mean that it should read $payments = $_POST['payments'>'0'];

Thanks!

Re: Code producing error message and I can't figure out why

Posted: Mon Jun 14, 2010 4:44 pm
by Benjamin
If the number of payments is not valid, it should be set to a known value. This line will set $payments to 1 if $_POST['payments'] is not set, is not a number, or is less than 1.

Code: Select all

$payments = (isset($_POST['payments']) && preg_match('#^\d+$#', $_POST['payments']) && $_POST['payments'] > 0) ? $_POST['payments'] : 1;