title: Failure message doesn't display when invalid PHP form is submitted
body: <p>I'm having some difficulty with a PHP form, the problem being that my confirmation message displays even when the form fields have not been validated.</p>
  
  <p>If the form has not been validated, I would like to display an alternative message that explains to the user that something went wrong and their message was not sent.</p>
  
  <p>Unfortunately, my understanding of PHP is only as good as the tutorials I've taken in trying to put this form together so I'm a bit stuck! Would someone be kind enough to explain to me what I am doing wrong?</p>
  
  <p>Here's the code:</p>
  
  <pre><code>&lt;?php
  ini_set('display_errors', 'On');
  
  //Defines error variables and sets to empty
  $senderErr = $senderEmailErr = $messageErr = "";
  //Defines text entry variables and sets to empty
  $sender = $senderEmail = $message = "";
  //Defines failure/confirmation messages and sets to empty
  $thankYou = "";
  $formFail = "";
  
  function test_input($data) {
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
  }
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
     if (empty($_POST["sender"])) {
       $senderErr = "Name is required";
     } else {
       $sender = test_input($_POST["sender"]);
       // Checks that name only contains letters and whitespace
       if (!preg_match("/^[a-zA-Z ]*$/",$sender)) {
         $senderErr = "Only letters and white space allowed";
       }
     }
  
     if (empty($_POST["senderEmail"])) {
       $senderEmailErr = "Email is required";
     } else {
       $senderEmail = test_input($_POST["senderEmail"]);
       // Checks if e-mail address syntax is valid
       if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$senderEmail)) {
         $senderEmailErr = "Invalid email format";
       }
     }
  
     if (empty($_POST["message"])) {
       $messageErr = "You haven't written anything";
     } else {
       $message = test_input($_POST["message"]);
       }
  
  if($_POST["submit"]) {
      $recipient="me@email.com";
      $subject="enquiry";
      $sender=$_POST["sender"];
      $senderEmail=$_POST["senderEmail"];
      $message=$_POST["message"];
  
      $mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";
  
      mail($recipient, $subject, $mailBody, "From: $sender &lt;$senderEmail&gt;");
  
      $thankYou = "Thanks for your message. We'll get back to you shortly";
      }
      else {
          $formFail = "Oops. Something went wrong. Please fill out the required fields.";
      }
  }
  ?&gt;
  </code></pre>
  
  <p>I think it has something to do with the fact that I'm not setting the right conditions for failure. Problem is, I'm not quite sure where to start. Apologies for being a noob. Help appreciated. Thanks.</p>
  
  <p>Edit - here's the HTML in response to Fred's question about whether the elements are named:</p>
  
  <pre><code>    &lt;form method="post" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt;
  
      &lt;label&gt;Name:&lt;/label&gt;&lt;/br&gt;
      &lt;input type="text" name="sender"&gt;&lt;/br&gt;
      &lt;span class="error"&gt;* &lt;?php echo $senderErr;?&gt;&lt;/span&gt;
      &lt;/br&gt;
  
      &lt;label&gt;Email address:&lt;/label&gt;&lt;/br&gt;
      &lt;input type="text" name="senderEmail"&gt;&lt;/br&gt;
      &lt;span class="error"&gt;* &lt;?php echo $senderEmailErr;?&gt;&lt;/span&gt;
      &lt;/br&gt;
  
      &lt;label&gt;Message:&lt;/label&gt;&lt;/br&gt;
      &lt;textarea rows="5" cols="20" name="message"&gt;&lt;/textarea&gt;&lt;/br&gt;
      &lt;span class="error"&gt;* &lt;?php echo $messageErr;?&gt;&lt;/span&gt;
  
      &lt;input type="submit" name="submit" value="submit"&gt;&lt;/br&gt;
  &lt;/form&gt;
  
  &lt;span&gt;&lt;?php echo $thankYou;?&gt;&lt;/span&gt;
  &lt;span&gt;&lt;?php echo $formFail;?&gt;&lt;/span&gt;
  </code></pre>