title: Why is this PHP validation still setting the value
body: <p>I am trying to validate a form field (to be used for First Name) for letters and dashes only, but regardless of how I implement the code, the value of $player_name is still being set.</p>
  
  <pre><code>if ($_SERVER["REQUEST_METHOD"] == "POST") {
              if (empty($_POST["player_name"])) {
                  $nameErr = "Name is required";
              } else {
                  $player_name = test_input($_POST["player_name"]);
                  // check if name only contains letters and whitespace
                  if (!preg_match("/^[a-zA-Z -]+$/",$player_name)) {
                      $nameErr = "Only letters and white space allowed"; 
                  }
              }
          }
  
          function test_input($data) {
             $data = trim($data);
             $data = stripslashes($data);
             $data = htmlspecialchars($data);
             return $data;
          }
  </code></pre>
  
  <p>The form has been implemented using the following code:</p>
  
  <pre><code>&lt;form method="post" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt; 
         Registration Date: &lt;input type="text" name="date_captured" id="date_captured" value="&lt;?php echo $current_date; ?&gt;"&gt;
         &lt;br&gt;&lt;br&gt;
         Name: &lt;input type="text" name="player_name" id="player_name"&gt;
         &lt;span class="error"&gt;* &lt;?php echo $nameErr;?&gt;&lt;/span&gt;
         &lt;br&gt;&lt;br&gt;
         &lt;input type="submit" name="submit" value="Submit"&gt; 
      &lt;/form&gt;
  
      &lt;?php
          echo "&lt;h2&gt;Your Input:&lt;/h2&gt;";
          echo $date_captured;
          echo $player_name;
          echo "&lt;br&gt;";
      ?&gt;
  </code></pre>
  
  <p>Even though the "Only letter and white space allowed" error is set and displayed, the value of $player_name is still set.</p>