0

Hello, everyone I am going to get right to the point.
I am trying to create this index.php with a discounted display of the calculation. Here is my display.php page code.
I have your classic $product_description, $list_price, and $discount_present. I think it might be working well the only problem that I am having is if the user is does not meet the validation requirements I am wanting the error message to display under the header.

<?php
    //get the data from the form
    $product_description = $_POST['product_description'];
    $list_price = $_POST['list_price'];
    $discount_percent = $_POST['discount_percent'];
    $sales_tax_rate = $_POST['.08']; 

    //Validate product description
    if(empty($product_description)){
        $error_message = 'Product Description is required.';}
        else if(!is_numeric($product_description)){
        $error_message = 'Product Description is NOT suppose to be a number';}

    //Validate list price
    if(empty($list_price)){
        $error_message = 'List Price is required.';}
    else if ($list_price <= 0){
        $error_message = 'List Price must be greater than zero';}

    //Validate discount percent
    if(empty($discount_percent)){
        $error_message = 'Discounted Percent is required.';}
    else if ($discount_percent <= 0){
        $error_message = 'Discounted Percent must be greater than zero';}

    //calculate the discount and discounted price
    $discount = $list_price * $discount_percent * .08;
    $discount_price = $list_price - $discount;
    $sales_tax_amount = $discount_price * .08;

    //apply currency formatting to the dollar and percent amounts
    $list_price_formatted = "$".number_format($list_price, 2);
    $discount_percent_formatted = $discount_percent."%";
    $discount_formatted = "$".number_format($discount, 2);
    $discount_price_formatted = "$".number_format($discount_price, 2);
    $sales_tax_rate_formatted = $sales_tax."%";
    $sales_tax_amount_formatted = "$".number_format($sales_tax_amount, 2);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3c.org/TR/xhtml1-transitional.dtd">
<html xmlns="http://www.w3c.org/1999/xhtml">

<head>

    <title>Product Discount Calculator</title>
    <link rel="stylesheet" type="text/css" href="styles.css" />
    </head>

    <body>
        <div id="content">
            <h1>Product Discount Calculator</h1>
             <p class="error"><?php echo $error_message; ?></p>

            <label>Product Description:</label>
            <span><?php echo $product_description; ?></span><br>

            <label>List Price:</label>
            <span><?php echo $list_price_formatted; ?></span><br>

            <label>Standard Discount:</label>
            <span><?php echo $discount_percent_formatted; ?></span><br>

            <label>Discount Amount:</label>
            <span><?php echo $discount_formatted; ?></span><br>

            <label>Discount Price:</label>
            <span><?php echo $discount_price_formatted; ?></span><br />
             <br />

            <label>Sales Tax Rate:</label>
            <span><?php echo $sales_tax_rate_formatted; ?></span><br />

            <label>Sales Tax Amount:</label>
            <span><?php echo $sales_tax_amount_formatted; ?></span>
        </div>
    </body>
</html>
3
Contributors
4
Replies
33
Views
3 Years
Discussion Span
Last Post by broj1
0

In your code your condition for validation is incorrect. I suppose product description is string.

//Validate product description

if(empty($product_description)){
    $error_message = 'Product Description is required.'; }
else if(!is_numeric($product_description)){
    $error_message = 'Product Description is NOT suppose to be a number';}

Case1
If $product_description is string, say "It is used for bla bla bla", then

is_numeric($product_description) results FALSE,
!is_numeric($product_description) results TRUE,

Hence your $error_message displays if you use echo $error_message.

Case2
If $product_description is string, say "12342", then

is_numeric($product_description) results TRUE,
!is_numeric($product_description) results FALSE,

Hence your $error_message will NOT be displayed.

Substitute following,

if(!empty($product_description) && isset($product_description) && is_string($product_description))
{
    // Valid condition.
}
else 
{
    echo "Product Description is in valid format.";
}
0

Rahual47,
Thank you I got it to work BUT I am still trying to get the error message to come up under the Header and if it comes up I need to form to not be processed. Any suggestions?

0

Maybe it needs to be in an if condition under the header <h1>Product Calculator</h1>?

0

You have to initialize the $error_message to empty string frst: $error_message = ''; so in case there are no errors the empty string will be displayed (= nothing will be displayed).

EDIT: Also you can check if error message exists and display it only then:

<?php 
if(isset($error_message) && !empty($error_message)) {
    echo '<p class="error">' . $error_message . '</p>';
}
?>

You might also add a Javascript checking for better user experience. This way you do not submit the data before it is OK (you still check it on the server, though).

A couple of other issues:

  • since the data entered in the form will be inserted into html you have to sanitize it using htmlspecialchars() function. This way all dangerous characters entered wont do any harm (i.e. <script>)
  • in the code where you use dollar sign as a currency you should use single quotes like $list_price_formatted = '$'.number_format($list_price, 2); or escape the $ sign. Otherwise php might expect a variable name following it.
  • before using any element of the $_POST array you better check if it exists (using isset() function).

Edited by broj1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.