我不小心删除了我先前关于此的问题。
我是PHP新手。这是我的第一份工作。我收到一条错误消息“销售价格必须是有效量”,该消息显示在第一个不应出现的输入框旁边。当用户单击“确认”而输入中没有数据时,就会发生这种情况。说明指出,在这种情况下,用户保留在页面上。我现在不需要显示任何错误消息。如何在没有输入的情况下单击“确认”,如何使错误消息按其应有的方式工作,但又不显示一条消息?希望这有道理。
// 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 ' $' . number_format($discount_amount, 2); ?></p>
<p class="total">Total Price <?php echo ' $' . 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>
答案 0 :(得分:1)
好的,所以您需要像这样构造表单处理逻辑:
$sales_price = ''; //default value
//other fields - I used just this one, but obviously you should do the
//same/simular for the rest of them
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//do stuff common to both form, such as both have sales_price
$sales_price = filter_input(INPUT_POST, 'sales_price', FILTER_VALIDATE_FLOAT);
if(isset($_POST['submitButton'])) {
//code specific to the first form
$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;
}
//and so on
}else if(isset($_POST['confirmSubmit'])){
//code specific to the second form
if(!empty($sales_price)){
//do stuff
}
}
}
这样,当第一个表单仅提交该代码时,第二个仅提交该代码。
如果isset($_POST['submitButton'])
为假,则无法运行验证代码,因此,提交其他表单时不会出现错误。您应该检查第二个表单处理代码中的数据,以确保第一个已发送。您还可以添加一个隐藏字段(用于确认表单),该字段在提交后的第一个为空,然后在提交后填充。
专业提示:您可以在第二种形式的处理代码中使用do / while循环作为控制结构,如下所示:
}else if(isset($_POST['confirmSubmit'])){
do{
if(empty($sales_price)) break; //bail on the loop
//form processing code
}while(false); //runs 1 time
Do while与其他循环不同,它在运行1次后会检查条件(在while部分中)。在这种情况下,条件为FALSE,因此它结束了循环。但这允许您使用break
或continue
(更合乎逻辑的是中断)退出循环并阻止其余代码运行。这样比较干净,然后使用if条件(IMO)排除了代码。
希望有帮助。
PS-我试图尽可能简化它。
答案 1 :(得分:0)
当用户点击“确认”时,如果 输入。
根据您的代码,我认为您真正需要的是在提交之前进行简单的Javascript
表单验证。您可以添加以下内容:
<head>
<script>
function validateForm() {
// check values
var salesPriceValue = document.forms["priceForm"]["sales_price"].value;
if (salesPriceValue.length<=0) {
alert("Please enter Sales Price before submitting the form!");
return false;
}
return true;
}
</script></head>
<body>
<form id="priceForm" name="priceForm" method="post" action='' onsubmit="return validateForm()">
<input type="text" id="sales_price" name="sales_price" required
value="<?php echo $sales_price; ?>"/>
.. etc
键是表单标签上的onsubmit
功能。您可以根据需要添加更多表单项验证。我只是显示一种输入文本类型的示例。