我不确定这个问题是否已经通过其他问题得到了回答,但是......
我正在为我的工作创建一个网页,其中包含多个输入字段(所有输入字段都具有相同的验证码)。以下是我的PHP代码的一小部分:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["bottomair"])) {
$bottomairerror = "Maketable bottom air temp is required";
} else {
$bottomair = test_input($_POST["bottomair"]);
if (!filter_var($bottomair, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$coldmin, "max_range"=>$coldmax)))) {
$bottomairerror = $errormsg;
}
}
if (empty($_POST["topair"])) {
$topairerror = "Maketable top air temp is required";
} else {
$topair = test_input($_POST["topair"]);
if (!filter_var($topair, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$coldmin, "max_range"=>$coldmax)))) {
$topairerror = $errormsg;
}
}
if (empty($_POST["meat"])) {
$meaterror = "Maketable meat temp is required";
} else {
$meat = test_input($_POST["meat"]);
if (!filter_var($meat, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$coldmin, "max_range"=>$coldmax)))) {
$meaterror = $errormsg;
}
}
if (empty($_POST["cheese"])) {
$cheeseerror = "Maketable cheese temp is required";
} else {
$cheese = test_input($_POST["cheese"]);
if (!filter_var($cheese, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$coldmin, "max_range"=>$coldmax)))) {
$cheeseerror = $errormsg;
}
}
if (empty($_POST["walkin"])) {
$walkinerror = "Walk-In temp is required";
} else {
$walkin = test_input($_POST["walkin"]);
if (!filter_var($walkin, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$coldmin, "max_range"=>$coldmax)))) {
$walkinerror = $errormsg;
}
}
if (empty($_POST["refrig1"])) {
$refrig1error = "Refrigerator #1 temp is required";
} else {
$refrig1 = test_input($_POST["refrig1"]);
if (!filter_var($refrig1, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$coldmin, "max_range"=>$coldmax)))) {
$refrig1error = $errormsg;
}
}
if (empty($_POST["refrig2"])) {
$refrig2error = "Refrigerator #2 temp is required";
} else {
$refrig2 = test_input($_POST["refrig2"]);
if (!filter_var($refrig2, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$coldmin, "max_range"=>$coldmax)))) {
$refrig2error = $errormsg;
}
}
if (empty($_POST["refrig3"])) {
$refrig3error = "Refrigerator #3 temp is required";
} else {
$refrig3 = test_input($_POST["refrig3"]);
if (!filter_var($refrig3, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$coldmin, "max_range"=>$coldmax)))) {
$refrig3error = $errormsg;
}
}
}
是否有一个函数可以用来“整理”我的PHP代码,以便在编码时,我没有那么多行来调试?
答案 0 :(得分:0)
我认为你这里只有三个选择:
答案 1 :(得分:0)
创建一个数组,其中包含必填字段的名称作为键,并将错误消息作为值。然后遍历这个数组。使用$_POST
超全局中的键查看是否已设置。如果未设置,则抛出您定义为值的错误消息。有点像:
$required_parameters = array(
"bottomair" => "Maketable bottom air temp is required",
"topair" => "Maketable top air temp is required",
// etc
);
$errorslist = array();
foreach($required_parameters as $key => $error)
{
if(empty($_POST[$key]))
{
$errorslist[] = $error; // append to list
}
else
{
$test = test_input($_POST[$key]);
if (!filter_var($test, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$coldmin, "max_range"=>$coldmax))))
{
$errorslist[] = $errormsg; // <-- I don't know where you define errormsg, though
}
}
}
然后,您也可以遍历$errorslist
数组,以显示列表中的所有错误。