title: Struggling with PHP validation
body: <p>I have some previous experience in Python programming, but i was asked to build a web page for someone so i have tried to build a basic page using a bootstrap framework, html and php (Learning as a went along).</p>
  
  <p>Everything seemed to be exactly as needed appart from the subscription form having no validation, so i thought it was best to impliment some, but the form doesnt seem to react and still allows the script to complete and send the email even with a total empty form.</p>
  
  <p>Would this be because the form is in a 'Modal'? I am still quite a novice at this and i apologise if this has been covered before but i cannot find an answer that helps me.</p>
  
  <p>I attach the modal form html and php code.</p>
  
  <pre><code>&lt;!-- Modal form --&gt;
  &lt;div class="modal fade" id="Subscribe" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt;
    &lt;div class="modal-dialog"&gt;
      &lt;div class="modal-content"&gt;
        &lt;div class="modal-header"&gt;
          &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
          &lt;h4 class="modal-title" id="myModalLabel"&gt;Subscribe for updates!&lt;/h4&gt;
        &lt;/div&gt;
        &lt;div class="modal-body"&gt;
              &lt;div class="container-fluid"&gt;
                &lt;form method="post" action="Submit.php"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-xs-12"&gt;
                          &lt;div class="input-group" style="margin-bottom: 5px;"&gt;
                              &lt;span class="input-group-addon" id="first"&gt;First Name&lt;/span&gt;
                              &lt;input type="text" class="form-control" placeholder="John" name="first" aria-describedby="First-Name"&gt;
                              &lt;span class="input-group-addon error" style="color: red"&gt;* &lt;?php echo $firstErr;?&gt;&lt;/span&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-xs-12"&gt;
                          &lt;div class="input-group" style="margin-bottom: 5px;"&gt;
                              &lt;span class="input-group-addon" id="last"&gt;Surname&lt;/span&gt;
                              &lt;input type="text" class="form-control" placeholder="Smith" name="surname" aria-describedby="Surname"&gt;
                              &lt;span class="input-group-addon error" style="color: red"&gt;* &lt;?php echo $lastErr;?&gt;&lt;/span&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                   &lt;/div&gt;
                   &lt;div class="row"&gt;
                      &lt;div class="col-xs-12"&gt;
                          &lt;div class="input-group" style="margin-bottom: 5px;"&gt;
                              &lt;span class="input-group-addon" id="email"&gt;Email&lt;/span&gt;
                              &lt;input type="email" class="form-control" placeholder="something@example.co.uk" name="email" aria-describedby="Email"&gt;
                              &lt;span class="input-group-addon error" style="color: red"&gt;* &lt;?php echo $fromErr;?&gt;&lt;/span&gt;
                          &lt;/div&gt;
                       &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-xs-12"&gt;
                          &lt;div class="input-group" style="margin-bottom: 5px;"&gt;
                              &lt;span class="input-group-addon" id="notes"&gt;Notes&lt;/span&gt;
                              &lt;textarea class="form-control" rows="3" placeholder="Please put anything else you want to say here" name="notes" aria-describedby="Notes"&gt;
              &lt;/textarea&gt;
                          &lt;/div&gt;
                       &lt;/div&gt;
                  &lt;/div&gt;
                &lt;div class="modal-footer"&gt;
                  &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
                  &lt;button type="submit" name="submit" value="submit" class="btn btn-primary"&gt;Send&lt;/button&gt;
                &lt;/div&gt;
              &lt;/form&gt;
            &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>File named 'Submit.php</p>
  
  <pre><code>&lt;?php 
  
  // define variables and set to empty values
  $fromErr = $firstErr = $lastErr = $notesErr = "";
  $from = $first_name = $last_name = $notes = "";
  
  function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  }
  
  if (isset($_POST['submit'])) {
    if (empty($_POST["email"])) {
      $emailErr = "Email is required";
    } else {
      $email = test_input($_POST["email"]);
      // check if e-mail address is well-formed
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "Invalid email format"; 
      }
    }
    if (empty($_POST["first"])) {
      $firstErr = "Name is required";
    } else {
      $first_name = test_input($_POST["first"]);
      // check if name only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
        $firstErr = "Only letters and white space allowed"; 
      }
    }
    if (empty($_POST["surname"])) {
      $lastErr = "Surname is required";
    } else {
      $last_name = test_input($_POST["surname"]);
      // check if name only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
        $lastErr = "Only letters and white space allowed"; 
      }
    }
      $notes = test_input($_POST["notes"]); // this is the senders message
  
      $to = "myemail@domain.com";  // this is your Email address
      $subject = "Website subscription from " . $first_name . " " . $last_name; //Subject line
  
      $message = "First name: " . $first_name . "&lt;br&gt;" . "Surname: " . $last_name . "&lt;br&gt;" . "Email: " . $from . "&lt;br&gt;" . "Notes: " . $notes;
  
      $headers = "From: " . $from . "\r\n";
  //  $headers .= "CC: myemail@domain.comr\n"; option to CC
      $headers .= "MIME-Version: 1.0\r\n";
      $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  
      mail($to,$subject,$message,$headers);
      header('Location: ./index.html#Thanks'); // redirect back to main page with 'Thanks' Modal open
  }
  ?&gt;
  </code></pre>
  
  <p>I would very much appreciate any help!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Just incase anyone wants to know how I totally ended up where I needed to be: @leepowers answer was a huge step in the right direction, this enabled me to check whether there were any errors and prevent the email sending if there were, I then learnt about 'sessions', i stored the errors in sessions (probably in a very messy way) so i could reuse them on return to the form.</p>
  
  <pre><code>// Gather all errors into an array
  $errors = array($fromErr, $firstErr, $lastErr);
  // Remove any empty error messages from the array
  $errors = array_filter($errors);
  // An array with more than zero elements evaluates `true` in a boolean context
  if ($errors) {
      $_SESSION["fromErr"] = $fromErr;
      $_SESSION["firstErr"] = $firstErr;
      $_SESSION["lastErr"] = $lastErr;
      die(header('Location: ./index.html#Subscribe'));
  } else { // send email
  </code></pre>
  
  <p>In my html page i have some JS to direct the 'redirect' into a modal:</p>
  
  <pre><code>&lt;script&gt;
  $(document).ready(function() {
  
    if(window.location.href.indexOf('#Thanks') != -1) {
      $('#Thanks').modal('show');
   } else if(window.location.href.indexOf('#Subscribe') != -1) {
      $('#Subscribe').modal('show');
    }
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>and now each of my input in my form have a  dedicated to errors:</p>
  
  <pre><code>&lt;div class="input-group" style="margin-bottom: 5px;"&gt;
                          &lt;span class="input-group-addon" id="email"&gt;Email&lt;/span&gt;
                          &lt;input type="email" class="form-control" placeholder="something@example.co.uk" name="email" aria-describedby="Email"&gt;
                          &lt;span class="input-group-addon error" style="color: red"&gt;* &lt;?php echo $_SESSION["fromErr"];?&gt;&lt;/span&gt;
  </code></pre>
  
  <p>Thank you all for the help.</p>