即使语句为真,其他语句仍在运行?

时间:2020-06-19 10:23:36

标签: javascript php html

我正在尝试为表单建立验证系统,而input元素之一称为verification。仅当verification的值为108时才可以提交表单(作为发送给他人的电子邮件)。但是,当我运行代码时,我发现else语句(嵌入式if语句的内部)中的代码也运行了,并且出现了一个弹出窗口,询问我3+105是什么。为什么是这样?

HTML

<form class="pb-5" action="" method="POST">
   <div class="form-row">
      <div class="form-group col-md-6">
         <label for="inputName4">Name</label>
         <input
            type="text"
            class="form-control"
            id="inputName"
            name="name" 
            value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES) : ''; ?>"
            required
            />
      </div>
      <div class="form-group col-md-6">
         <label for="inputCompany">Company</label>
         <input
            type="text"
            class="form-control"
            id="inputCompany"
            name="company" 
            value="<?php echo isset($_POST['company']) ? htmlspecialchars($_POST['company'], ENT_QUOTES) : ''; ?>"
            required
            />
      </div>
   </div>
   <div class="form-row">
      <div class="form-group col-md-6">
         <label for="subject">Subject</label>
         <input
            type="text"
            class="form-control"
            id="subject"
            name="subject" 
            value="<?php echo isset($_POST['subject']) ? htmlspecialchars($_POST['subject'], ENT_QUOTES) : ''; ?>"
            required
            />
      </div>
      <div class="form-group col-md-6">
         <label for="inputEmail">Email</label>
         <input
            type="email"
            class="form-control"
            id="inputEmail"
            name="email" 
            value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email'], ENT_QUOTES) : ''; ?>"
            required
            />
      </div>
   </div>
   <div class="form-group">
      <label for="verification">What is 5+103?</label>
      <input
         type="text"
         class="form-control"
         id="verification"
         name="verification" 
         required
         ></input>
   </div>
   <div class="form-group">
      <label for="textarea">Message</label>
      <textarea
         class="form-control"
         id="textarea"
         rows="3"
         name="message" 
         required
         ><?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message'], ENT_QUOTES) : ''; ?>"</textarea>
   </div>
   <button type="submit" class="btn btn-primary my-3">
   Send
   </button>
</form>

PHP

    $verification = $_POST["verification"];
    settype($verification, "integer");
    if($_SERVER["REQUEST_METHOD"] == "POST") {
         if($verification == 108) {
              mail($to, $subject, $message, $headers);
              echo "<script> alert('Thank you for your mail!')</script>";
         } elseif ($_SERVER["REQUEST_METHOD"] == "POST" && mail($to, $subject, $message, $headers) === false) {
              echo "<script> alert('An error has occurred.')</script>";
         } else {
              echo "<script> alert('What is 3+105?')</script>";
         }
    } else {
         return;
    }

如果我能以任何方式澄清我的问题,请随时让我知道!预先感谢!

1 个答案:

答案 0 :(得分:2)

我想您必须重写if ... else ...

$verification = $_POST["verification"];
settype($verification, "integer");
if($_SERVER["REQUEST_METHOD"] == "POST") {
   if($verification == 108) {
      //TEST IF MAIL IS CORRECTLY SEND
      if( mail($to, $subject, $message, $headers) === false){
         echo "<script> alert('An error has occurred.')</script>";
         }
      else{
         echo "<script> alert('Thank you for your mail!')</script>";
         }
   }
   else {
      echo "<script> alert('What is 3+105?')</script>";
   }
}