我不能在Javascript中的多个条件中使用if..else语句

时间:2019-10-16 18:28:55

标签: javascript php jquery json ajax

超级初学者。我正在使用Javascript,Json和后端PHP进行联系。当某些字段保留为空时,我无法使条件条件起作用,然后所有警报消息都层出不穷。

我已经尝试在每个if语句之后使用“ else”,但是它仅适用于第一个条件。

      if(result.validAllEmpty == "allEmpty"){ 
         alert("Name, Email and Message are required");
             $("#yname").css({"border":"5px solid red"});
             $("#nameSpan").text(" must only contain letters and 
                whitespace, ex.\"John Doe\"");
             $("#yemail").css({"border": "5px solid red"});
             $("#emailSpan").text("  please enter a valid email address, 
               ex.\"paulo@brazil.com\"");
             $("#ymessage").css({"border":"10px solid red"});
             $("#messageSpan").text("please type something, anything");



        } else {



        if(result.validFormName == "empty" && result.validFormEmail == "empty"){
          alert("Name and Email are required");
           $("#yname").css({"border":"5px solid red"});
            $("#nameSpan").text(" must only contain letters and whitespace, ex.\"John Doe\"");
            $("#yemail").css({"border": "5px solid red"});
            $("#emailSpan").text("  please enter a valid email address, ex.\"paulo@brazil.com\"");


        } 

         if(result.validFormName == "empty"){
            alert("Name is required");
          $("#yname").css({"border":"5px solid red"});
          $("#nameSpan").text(" must only contain letters and whitespace, ex.\"John Doe\"");

        }

         if(result.validFormEmail == "empty"){
          alert("Email is required");
          $("#yemail").css({"border": "10px solid red"});
          $("emailSpan").text("please enter a valid email address, ex.\"paulo@brazil.com\"");

          }   

        if(result.validFormName == "empty" && result.validFormMessage == "empty"){
            alert("Name and Message are required");
            $("#yname").css({"border":"5px solid red"});
            $("#nameSpan").text("must only contain letters and whitespace");
            $("#ymessage").css({"border":"10px solid red"});
            $("#messageSpan").text("type something, anything");

        }    

         if(result.validFormEmail == "empty" && result.validFormMessage == "empty"){
            alert("Email and Message are required");
          $("#yemail").css({"border": "5px solid red"});
          $("#emailSpan").text(" required  please enter your email address");
          $("#ymessage").css({"border": "10px solid red"});
          $("#messageSpan").text("write something, anything..");

        }        

          if(result.validFormMessage == "empty"){
          alert("Message is required");
          $("#ymessage").css({"border": "10px solid red"});
          $("#messageSpan").text(" write something, anything");

        }


SOME PHP

    } else { 
               //validate all empty
           if (empty ($name) && empty ($email) && empty ($message)){
                $export['validAllEmpty'] = "allEmpty";
           }
                if (empty($name)){

                    $export['validFormName'] = "empty";

                    } else { 

                    $export['validFormName'] = "notEmpty";

                    }

                if (empty($email)){

                    $export['validFormEmail'] = "empty";

                    } else {

                    $export['validFormEmail'] ="notEmpty";

                    } 

                if (empty($message)){

                    $export['validFormMessage'] = "empty";

                } else {

                $export['validFormMessage'] = "notEmpty";

                } 
         }

        }   

       echo json_encode($export);

     }





?>

我希望警报消息仅针对相关条件显示,而不是连续显示。另外,有没有办法使此代码更简单?抱歉,如果帖子和代码混乱。我第一次在这里发帖。非常感谢您。谢谢

0 个答案:

没有答案