我的代码检查是否有任何字段未输入,然后检查验证问题是当没有错误时它仍然显示第一个error_message。=
E.g
$error_message .= "<div class=\"error-header\">Error: You did not submit the following required information.</div>";
$error_message .= "<ul id=\"errors\">"
// this part in the second else statement always shows how do i stop that;
完整代码:
if((!$email_address)||(!$password)||(!$confirm_password)||(!$first_name)||(!$last_name)||(!$address)||(!$postal_code)||(!$region)||(!$country)) {
$error_message .= "<div class=\"error-header\">Error: You did not submit the following required information.</div>";
$error_message .= "<ul id=\"errors\">";
if(!$email_address) {
$error_message .= "<li>Please enter you're email address.</li>";
}
etc...
}else {
$error_message .= "<div class=\"error-header\">Error: The following information is invalid.</div>";
$error_message .= "<ul id=\"errors\">";
if($row_count > 0) {
$error_message .= "<li>The email address you provided is already in use, please use another email address.</li>";
}
if($confirm_password != $password) {
$error_message .= "<li>The passwords do not match.</li>";
}
if(!isset($_POST["interests"]) || count($_POST["interests"]) < 3) {
$error_message .= "<li>Please choose three departments that are of interest to you.</li>";
}else{
$interests = $_POST["interests"];
}
if(strtolower($captcha_code) == strtolower((string)$_SESSION["captcha"])) {
$error_message .= "<li>The code you entered is incorrect.</li>";
}
$error_message .= "</ul>";
}
答案 0 :(得分:2)
稍微修改了你的代码:
if((!$email_address)||(!$password)||(!$confirm_password)||(!$first_name)||(!$last_name)||(!$address)||(!$postal_code)||(!$region)||(!$country)) {
$error_message .= "<div class=\"error-header\">Error: You did not submit the following required information.</div>";
$error_message .= "<ul id=\"errors\">";
if(!$email_address) {
$error_message .= "<li>Please enter you're email address.</li>";
}
etc...
}else {
$error_message="";
if($row_count > 0) {
$error_message .= "<li>The email address you provided is already in use, please use another email address.</li>";
}
if($confirm_password != $password) {
$error_message .= "<li>The passwords do not match.</li>";
}
if(!isset($_POST["interests"]) || count($_POST["interests"]) < 3) {
$error_message .= "<li>Please choose three departments that are of interest to you.</li>";
}else{
$interests = $_POST["interests"];
}
if(strtolower($captcha_code) == strtolower((string)$_SESSION["captcha"])) {
$error_message .= "<li>The code you entered is incorrect.</li>";
}
//check if there is any error message, then create the div
if(!empty($error_message))
{
$errorMessageLi=$error_message;
$error_message= "<div class=\"error-header\">Error: The following information is invalid.</div>";
$error_message .= "<ul id=\"errors\">";
$error_message .= $errorMessageLi;
$error_message .= "</ul>";
}
}
答案 1 :(得分:0)
在我看来,这总是被执行。您需要控制何时显示$error_message
(此处未显示),或者您需要对该else块进行一些检查,以确定您是否实际触及了任何if语句。
if (!($row_count = 0) && !($confirm_password != $password) && ...) {$error_message .= "";}
这将是一种笨重的方式;你可以很容易地检查最后是否添加了<li>
,如果没有,请清除它;或者将一个计数器变量放入并在每个if块中递增(如果为真)。
答案 2 :(得分:0)
我倾向于使用数组来做这些事情:
$errors = array();
if (condition1) {
$errors[] = 'condition 1 was not met';
}
if (condition2) {
$errors[] = 'condition 2 was not met';
}
etc...
if (count($errors) > 0) {
display error messages
}