title: Problems with form validation using PHP
body: <p>I have a simple form on my website that I need to do server side validation on. If user enters incorrect data or leaves a field blank, the user should be re-shown the form with their submitted data pre-filled into the fields.</p>
  
  <p>My form code looks like this</p>
  
  <pre><code>&lt;!--Contact form--&gt;
          &lt;form name="contactform" action="validate.php" method="post"&gt;
              Name: 
              &lt;input type="text" name="name" value=""&gt;&lt;br&gt;
              &lt;span class="error"&gt;* &lt;?php echo $nameError; ?&gt;&lt;/span&gt;&lt;br&gt;
  
              Email: 
              &lt;input type="text" name="email" value=""&gt;
              &lt;span class="error"&gt;* &lt;?php echo $emailError; ?&gt;&lt;/span&gt;&lt;br&gt;
  
              Category:
              &lt;select name="category"&gt;
                  &lt;option value="" disabled selected&gt;Select&lt;/option&gt;
                  &lt;option value="shipping"&gt;Sales&lt;/option&gt;
                  &lt;option value="returns"&gt;Returns&lt;/option&gt;
                  &lt;option value="shipping"&gt;Shipping&lt;/option&gt;
                  &lt;option value="other"&gt;Other&lt;/option&gt;
              &lt;/select&gt;&lt;br&gt;
  
              &lt;textarea rows="10" cols="40" name="message" placeholder = "Send us a message"&gt;&lt;/textarea&gt;&lt;br&gt;
              &lt;input type="submit" name="submit" value="Submit" onclick="validate()"&gt;
  
          &lt;/form&gt;
  </code></pre>
  
  <p>At the moment all I'm testing to validate is the name and email fields. My problem is that when I load the page I immediately get errors saying "Notice: Undefined variable: nameError " which has to do with these lines in the code</p>
  
  <pre><code>&lt;span class="error"&gt;* &lt;?php echo $nameError; ?&gt;&lt;/span&gt;&lt;br&gt;  
  &lt;span class="error"&gt;* &lt;?php echo $emailError; ?&gt;&lt;/span&gt;&lt;br&gt;
  </code></pre>
  
  <p>However I know the validations are working because I placed echo statements in my validate.php file to test this. If I submit the form with incorrect or correct data, it will take me to a blank validate.php page that just displays the echo statements </p>
  
  <p>Here is my code for validate.php
      
  
  <pre><code>## Initialise varialbes to null ##
  $nameError ="";
  $emailError ="";
  $categoryError ="";
  $messageError ="";
  
  $name = $_POST['name'];
  
  ## On submitting form below function will execute ##
  if(isset($_POST['submit'])){
  if (empty($_POST["name"])) {
      $nameError = "Name is required";
      echo "name required";
      echo " ";
  } else {
      //$name = test_input($_POST["name"]);
      //check if name only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
          $nameError ="Only letters and white space allowed";
          echo "Invalid name";
      }
  }
  
  if(empty($_POST["email"])) {
      $emailError = "Email is required";
      echo "email required";
  } else {
      //$email = test_input($_POST["email"]);
      //check if email syntax is valid or not
      if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]/", $email)) {
          $emailError = "You did not enter a vaild email";
          echo "invalid email";
      }
  }
  }
  
  ?&gt;
  </code></pre>
  
  <p>So I know my validations are working, the problem is that they are not displaying on the form itself. Can anyone offer a solution on how to fix this?
  I've included a picture of one of the error messages I get when I load up the page <a href="http://i.stack.imgur.com/YgtOd.png" rel="nofollow">Error message on page load</a></p>