title: Display error if empty field - PHP
body: <p>I want to display an error on the same page if any field is empty.</p>
  
  <p>I've got this, which works but the empty error is displayed as soon as the page is loaded instead of appearing once empty fields are submitted.</p>
  
  <pre><code>&lt;?php
      // Required field names
      $required = array('triangleSide1', 'triangleSide2', 'triangleSide3');
  
      // Loop over field names, make sure each one exists and is not empty
      $error = false;
      foreach($required as $field) 
      {
          if (empty($_POST[$field])) 
          {
              $error = true;
          }
      }
  
      if ($error) 
      {
          echo "ALL FIELDS ARE REQUIRED";
      } 
      else 
      {
          echo header('Location: formSuccess.php');
      }
  ?&gt;
  </code></pre>
  
  <p>Any ideas? -- UPDATE // IVE TRIED ALL ANSWERS, nothing has worked so far</p>