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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
canadian_angel
Forum Commoner
Posts: 31
Joined: Fri Jun 11, 2010 3:13 pm

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

Post 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>
Last edited by Benjamin on Mon Jun 14, 2010 2:12 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post by Benjamin »

$_POST['payments'] must be a number greater than 0.
canadian_angel
Forum Commoner
Posts: 31
Joined: Fri Jun 11, 2010 3:13 pm

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

Post by canadian_angel »

Ok so srry does that mean that it should read $payments = $_POST['payments'>'0'];

Thanks!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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;
Post Reply