首页 > 解决方案 > PHP - 在错误的时间显示带有输入的警报

问题描述

我是 PHP 的新手,正在做我的第一份大学作业。我有一个无法解决的问题。我在 index.php 页面上有警报,仅当用户尝试提交数据以进行确认并且数据丢失或不正确时才会出现。目前,当页面加载时,警报会按应有的方式工作,并且仅在用户尝试“计算”缺少或不正确数据的页面时才会出现。计算后,用户点击“确认”按钮,将他们带到第二页confirm.php ..从那里用户应该能够点击“返回”并返回第一页。这可以正常工作,但是现在当用户返回第一页时,我在输入框旁边有警报。此时不应显示它们。我不知道如何纠正这个。请帮忙?

<?php
/**
 * Student: Sherrie Teague
 * Server-Side Programming
 * Date: 2/6/2019
 * HW2 - Quote
 */


/*
 * Course : Server-Side Programming
 * Student: Sherrie Teague
 * Assignment: HW2 - Quote
 * Date : 2/6/2019
*/

// 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';
    // or store it in a variable and post later
    $validation_error = 'Validation Error';
}

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

$discount_valid = true;
$discount_percentError = '';
// validate discount_percent
if ($discount_percent === FALSE) {
    $discount_percentError = 'Discount percent must be a valid amount';
    $discount_valid = false;
} else if ($discount_percent < 1.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="confirm.php">-->
    <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>

标签: phpalerts

解决方案


推荐阅读