提问人:Auron Kelmud 提问时间:2/7/2019 最后编辑:Greg SchmidtAuron Kelmud 更新时间:2/7/2019 访问量:152
每当在 if 循环中更改变量时,将布尔值从 false 切换到 true,如果更改了包含形式
Switching boolean from false to true whenever a variable is being changed within a if loop, if changed include form
问:
我将错误捕获变量设置为false,因此每当页面加载时,都不会抛出任何错误消息。我有一个变量,最初设置为初始值设定项,例如 .每当出现错误时,我希望设置为每个错误正在回声的消息,如果不等于最初初始化的消息,那么它应该包括我的表单页面,以允许用户重试。$error
$error = " ";
$error
$error
我尝试的是将变量设置为指定错误的新字符串,例如“你不能让价格框留空!”,在我的初始if循环的末尾,还有另一个if循环检查是否被更改,如果是,则再次包含表单页面。截至目前,我的变量从未更改,只是保持最初设置的状态。$error
$error
$pError = false;
$cError = false;
$error = " ";
if(!isset($_GET['pPrice']) || (!isset($_GET['cPrice']))){ //if not set include form
include "form.php";
}
if(isset($_GET['pPrice']) && isset($_GET['cPrice'])){ //if set get variables from form
$pPrice = $_GET['pPrice'];
$cPrice = $_GET['cPrice'];
$validateP = filter_input(INPUT_GET, 'pPrice', FILTER_VALIDATE_FLOAT); //checks to see if value is a float
$validateC = filter_input(INPUT_GET, 'cPrice', FILTER_VALIDATE_FLOAT); //checks to see if value is a float
if(empty($_GET['pPrice']) && (empty($_GET['cPrice']))){ //both fields empty
$error = "<h2 class='error'>Please make sure you fill out all the fields!</h2>";
}
else if(empty($_GET['pPrice'])){ //first field empty
$error = "<h2 class='error'>Previous price can't be empty!</h2>";
}
else if(empty($_GET['cPrice'])){ //second field empty
$error = "<h2 class='error'>Current price can't be empty!</h2>";
}
else if(!$validateP && !$validateC){ //both fields have incorrect inputs
$error = "<h2 class='error'>Please enter numbers only!</h2>";
}
else if(!$validateP){ //first field has a incorrect input
$error = "<h2 class='error'>Previous Price must be a valid number!</h2>";
}
else if(!$validateC){ //second field has a incorrect input
$error = "<h2 class='error'>Current Price must be a valid number!</h2>";
}
else {
echo "Your results have been submitted!";
}
if (!$error == " ") {
$pError == true;
$cError == true;
echo $error;
include "form.php";
} else{
$pError = false;
$cError = false;
}
}
预期结果是,如果用户输入了不正确的值或根本不输入值,请更新该特定错误的变量$error,将布尔值从 false 更改为 true,然后允许再次尝试。如果没有错误,则回显“成功”消息。
答: 暂无答案
评论
count