Linked Questions

Popular Questions

PHP - stop the notice/error displaying at wrong time

Asked by At

I accidentally deleted my previous question about this.

I'm new at PHP. This is my first assignment. I have an error message "Sales price must be a valid amount" that is being displayed next to the first input box that shouldn't be there. This occurs when the user clicks "confirm" without data in the inputs. Instructions state that in this case, the user remains on the page. I don't need any error messages displaying at this time. How can I keep my error messages working as they should but stop the one message from showing up when "confirm" is clicked with no input? Hope this makes sense.

// get the data from the form
$sales_price = filter_input(INPUT_POST, 'sales_price', FILTER_VALIDATE_FLOAT);
$discount_percent = filter_input(INPUT_POST, 'discount_percent', FILTER_VALIDATE_FLOAT);
$total_price = filter_input(INPUT_POST, 'total_price', FILTER_VALIDATE_FLOAT);


if (isset($_POST['confirmSubmit'])) {
    echo 'Validation Error';
    $validation_error = 'Validation Error';
}

// validate sales price
$sales_valid = true;
$sales_priceError = '';
if ($sales_price === NULL) {
    $sales_priceError = '';
    $sales_valid = false;
} else if ($sales_price === FALSE) {
    $sales_priceError = 'Sales price must be a valid amount';
    $sales_valid = false;
} else if ($sales_price <= 0.0) {
    $sales_priceError = 'Sales price must be greater than 0';
    $sales_valid = false;
}

// validate discount percent
$discount_valid = true;
$discount_percentError = '';
if ($discount_percent === NULL) {
    $discount_percentError = '';
    $discount_valid = false;
} else if ($discount_percent === FALSE) {
    $discount_percentError = 'Discount percent must be a valid amount';
    $discount_valid = false;
} else if ($discount_percent <= 0.0) {
    $discount_percentError = 'Discount percent must be greater than 0';
    $discount_valid = false;
}


// calculate the discount and the discounted price
$discount_amount = $sales_price * $discount_percent / 100;
$total_price = $sales_price - $discount_amount;

?>

<!doctype html>
<html lang="en">
<head>
    <title>Quote</title>
    <link rel="stylesheet" type="text/css" href="quote.css">
</head>
<body>
<section>
    <h1>Price Quotation</h1>
    <form id="priceForm" name="priceForm" method="post" action=''>
        <label for="sales_price">Sales Price </label>
        <input type="text" id="sales_price" name="sales_price" required
               value="<?php echo $sales_price; ?>"/>
        <?php if (!empty($sales_priceError)) : ?>
            <span style="color:red;background-color: white">
                    <?php echo $sales_priceError; ?>
            </span>
        <?php endif; ?>
        <br/>
        <br/>
        <label for="discount_percent">Discount Percent </label>
        <input type="text" id="discount_percent" name="discount_percent" required
               value="<?php echo $discount_percent; ?>"/>
        <?php if (!empty($discount_percentError)) : ?>
            <span style="color:red;background-color: white">
                    <?php echo $discount_percentError; ?>
                </span>
        <?php endif; ?>
        <p class="discount">Discount
            Amount <?php echo '&nbsp;&nbsp;&nbsp;&nbsp;$' . number_format($discount_amount, 2); ?></p>
        <p class="total">Total Price <?php echo '&nbsp;&nbsp;&nbsp;&nbsp;$' . number_format($total_price, 2); ?></p>
        <input type="submit" class=inline name="submitButton" id="submitButton" value="Calculate"/>
    </form>


    <form id="confirmForm" name="confirmForm" method="post" action="<?php echo(($sales_valid && $discount_valid) ? 'confirm.php' : ''); ?>">
    <input type="hidden" id="sales_price" name="sales_price" value="<?php echo $sales_price ?>"/>
    <input type="hidden" id="discount_amount" name="discount_amount" value="<?php echo $discount_amount ?>"/>
    <input type="hidden" id="total_price" name="total_price" value="<?php echo $total_price ?>"/>
    <input type="submit" class=inline name="confirmSubmit" id="confirmSubmit" value="Confirm"/>
    </form>
    <div>
        <p> Enter price and discount amount and click Calculate</p>
    </div>
</section>
</body>
</html>

Related Questions