title: PHP Form validation using 'if'
body: <p>I'm currently building a very small 'contact' form for use on a personal site.</p>
  
  <p>The form works, and each validation 'if' statement works individually, however, if for example I input a valid email address and phone number but leave the message blank, the email still sends and I get the success message. </p>
  
  <p>My guess would be to include the small 'if' statements into the one checking whether my required fields are not empty, though i'm not sure how to do this correctly as it is nesting multiple 'if's into one.</p>
  
  <p>Cheers</p>
  
  <pre><code>&lt;?php 
              // Validation goes here
              $errors = '';
              $success = 'Success! Your message has been sent. You should receive a reply within 48 hours.';
  
              $email = $_POST['email']; 
              $name = $_POST['thename']; 
              $comments = $_POST['comments'];
              $number = $_POST['number']; 
  
              if(empty($name) || empty($email) || empty($comments)) {
                  $errors .= "Error: please input a name, email address and your message.";
              } else {
                  $errors = '';
              }
  
              if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
                  $errors .= "Error: Invalid email address";
              } else {
                  $errors = '';
              }
  
              if (!preg_match("/^\(?0( *\d\)?){9,10}$/", $number)) {
                  $errors .= "Error: Invalid phone number";
              } else {
                  $errors = '';
              }
  
          ?&gt;
  
          &lt;!-- Display red error box or green success box depending on which is true --&gt;
          &lt;?php if(!empty($errors)): ?&gt;
              &lt;div class="validationbox | errorsbox"&gt;
                  &lt;?php echo $errors; ?&gt;
              &lt;/div&gt;
          &lt;?php elseif(empty($errors)): ?&gt;
              &lt;div class="validationbox | successbox"&gt;
                  &lt;?php echo $success; ?&gt;
              &lt;/div&gt;
              &lt;?php 
                  $message = ''; // Blank message to start with so we can append to it.
  
                  // Construct the message
                  $message .= "
                      Name: {$_POST['thename']};
                      Email: {$_POST['email']};
                      Number: {$_POST['number']};
                      Enquiry-type: {$_POST['enquiry-options']};
                      Message: {$_POST['comments']};
  ";
                  // test@testdomain.com
                  $to = 'test-email-deleted-for-stackoverflow'; 
                  $subject = 'Message from Portfolio';
                  $from = $_POST['thename'];
                  // YourSite@domain.com
                  $fromEmail = 'test-email-deleted-for-stackoverflow';
                  $header = 'From: ' . $from . '&lt;' . $fromEmail . '&gt;';
                  mail($to,$subject,$message,$header);
              ?&gt;
          &lt;?php endif; ?&gt;
      &lt;?php endif; ?&gt;
  </code></pre>