0

我下面的代码检查是否有任何未输入的字段,然后检查验证问题是当没有错误时它仍然显示第一个 error_message .=

例如

$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>";
     }
4

3 回答 3

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>";
            }
         }
于 2013-02-16T04:37:08.813 回答
0

在我看来,这总是被执行的。您要么需要控制何时$error_message显示(此处未显示),要么需要在 else 块中进行一些检查,以查看您是否真的点击了这些 if 语句。

if (!($row_count = 0) && !($confirm_password != $password) && ...) {$error_message .= "";}

那将是一种笨拙的方法。您可以在最后轻松检查是否<li>添加了任何内容,如果没有,请将其清除;或者放入一个计数器变量并在每个 if 块中递增它(如果为真)。

于 2013-02-16T04:29:56.883 回答
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
}
于 2013-02-16T04:42:07.220 回答