PHP Form Validation
<p>I'm trying to create my first php script for a contact form and it is always showing the $errors variable even when i first load the page. How can i make it so the $errors variable is only populated and displayed, after a user tried to submit the form without filling in the required fields?</p>

<pre><code>&lt;form method="post" action="&lt;?php $_SERVER['PHP_SELF']; ?&gt;"&gt;
  &lt;?php
    if(!is_null($errors)) {
        echo "&lt;h3&gt;$errors&lt;/h3&gt;";
    }
  ?&gt;

  &lt;label for="Name"&gt;Name&lt;/label&gt;&lt;em&gt;(Required)&lt;/em&gt;
  &lt;input type="text" name="Name"/&gt;

  &lt;label for="Email"&gt;Email&lt;/label&gt;&lt;em&gt;(Required)&lt;/em&gt;
  &lt;input type="text" name="Email"/&gt;

  &lt;label for="Website"&gt;Website&lt;/label&gt;
  &lt;input type="text" name="Website"/&gt;

  &lt;label for="Message"&gt;Message&lt;/label&gt;&lt;em&gt;(Required)&lt;/em&gt;
  &lt;textarea name="Message"&gt;&lt;/textarea&gt;

  &lt;button id="submit" type="submit" value="submit"name="submit"&gt;Submit&lt;/button&gt;
  &lt;input type="hidden" name="Submit" /&gt;
&lt;/form&gt;
</code></pre>

<p>and my script to handle the form</p>

<pre><code>$errors = null;

if(isset($_POST['submit'])) {   

  if(!empty($_POST['Name']) &amp;&amp; !empty($_POST['Email']) &amp;&amp; !empty($_POST['Message'])) {

    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $website = $_POST['Website'];
    $message = $_POST['Message'];

    //strip any html tags and slashes off of the fields
    $nameClean = strip_tags($name);
    $emailClean = strip_tags($email);
    $websiteClean = strip_tags($website);
    $messageClean = strip_tags($message);
    $messageCleanAndStripped = stripslashes($messageClean);
  }

} else {
    $errors = "Please fill in the required fields";
}
</code></pre>
