如果数据丢失或不正确,请使用PHP停止页面提交
我几乎完成了我的第一个大学PHP作业。这是一个头痛的问题,但我正处于最后阶段。我现在遇到的问题是验证。我已验证输入框是否包含适当的数据。说明告诉我,如果用户在没有输入数据的情况下点击“确认”按钮,用户将被重定向回index.php(同一页面)。现在,用户可以单击“确认”,即使输入框为空,它也会将它们引导到下一页。我尝试了多种方法,但都没有成功。我已经把最近的尝试注释掉了。我在网上找到的很多例子让我感到困惑,因为它们还包括我已经做过的验证。我还发现了很多使用JavaScript的例子。我必须使用PHP进行所有验证。非常感谢您的帮助 我已经更新了我的代码,现在我有它的工作。但另一个问题已经产生。在输入数据之前,错误消息会出现在输入框旁边。。而不是在提交数据丢失或不准确的页面后出现如果数据丢失或不正确,请使用PHP停止页面提交,php,validation,Php,Validation,我几乎完成了我的第一个大学PHP作业。这是一个头痛的问题,但我正处于最后阶段。我现在遇到的问题是验证。我已验证输入框是否包含适当的数据。说明告诉我,如果用户在没有输入数据的情况下点击“确认”按钮,用户将被重定向回index.php(同一页面)。现在,用户可以单击“确认”,即使输入框为空,它也会将它们引导到下一页。我尝试了多种方法,但都没有成功。我已经把最近的尝试注释掉了。我在网上找到的很多例子让我感到困惑,因为它们还包括我已经做过的验证。我还发现了很多使用JavaScript的例子。我必须使用P
引用
报价单
销售价格
总价
也许是这样的:
IF ( got a submission ) {
do validations
IF (!valid) {
show errors or message
}
ELSE { // its all ok
show confirmation html (could be included file)
}
}
ELSE { // no submission - first time here
show initial html
}
另一种看待它的方式是:
现在,您的表单提交将转到confirm.php。可以在index.php中进行验证,如果有效,则重定向到confirm.php,但不需要让提交表单的人在confirm.php结束验证
如果将验证留在confirm.php中,那么如果验证失败,则必须重定向回index.php。你可以使用重定向。这也可能有助于:
就个人而言,我更喜欢将其保存在一个文件中。使用此文件并尝试提交您的表格。如果你还面临这个问题,请告诉我
如果($\u POST&&$\u POST['sales\u price']!=''&&$\u POST['discount\u amount']!=''){
//转到下一页
var_dump($_POST);exit;
}否则{
//返回索引页
//标题('位置:')
}您可以执行以下操作:
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;
}
感谢您的回复,但这不起作用。else语句将被拒绝。“语句的正文为空”。我应该用另一种方式键入代码吗?你必须用这种方式使用if和else条件,调整你自己的代码。在其他情况下,请使用标题('位置:此处的主页路径');非常感谢。这是工作,但我的网页上收到错误消息。。告诉我有未定义的变量。。在第18、21、29和32行。变量为$sales\u price和$折扣\u percent需要确保您包含代码的其余部分:$sales\u price=filter\u input(input\u POST,'sales\u price',filter\u VALIDATE\u FLOAT)$折扣百分比=过滤输入(输入后,“折扣百分比”,过滤验证浮动)$总价=过滤输入(输入后,“总价”,过滤验证浮动)请原谅我之前的评论。我只需要将代码粘贴到我命名变量的下面。非常感谢你。它现在工作得很好!我投票支持你。我被告知它是有价值的,但不会公开显示,因为我是这个网站的新手。没问题:)你应该标记正确的答案,而不是投票。这样,如果另一个用户来到这个线程,他们知道什么答案解决了这个问题。我刚刚意识到,当我点击第二页上的“返回”,它会将我送回第一页。。。我的错误消息现在显示在输入框的右侧。。只有当数据无效但在输入任何数据之前它们现在才出现时,它们才应出现。你知道我该怎么纠正这个吗?非常感谢。
<form id="confirmForm" name="confirmForm" method="post" action="<?php echo ( ( $sales_valid && $discount_valid ) ? 'confirm.php' : '' ); ?>">
<?php
// set default values
$sales_price = '';
$sales_valid = true;
$sales_priceError = '';
$discount_percent = '';
$discount_valid = true;
$discount_percentError = '';
$discount_amount = 0;
$total_price = 0;
$validation_error = '';
// check for validation error submit
if( isset( $_POST['confirmSubmit'] ) ) {
$validation_error = 'Validation Error';
$discount_valid = false;
$sales_valid = false;
}
// check for submit
if( isset( $_POST['submitButton'] ) ) {
$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 ($sales_price === FALSE || $sales_price == '') {
$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;
}
// validate discount_percent
if ($discount_percent === FALSE || $discount_percent == '') {
$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" value="<?php echo $sales_price; ?>" required />
<?php
if ( ! empty($sales_priceError) ) { ?>
<span style="color:red;background-color: white">
<?php echo $sales_priceError; ?>
</span>
<?php } ?>
<br/>
<br/>
<label for="discount_percent">Discount Percent </label>
<input type="text" id="discount_percent" name="discount_percent" value="<?php echo $discount_percent; ?>" required"/>
<?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 ( ( isset( $_POST['confirmSubmit']) && $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>