title: PHP - Redisplay forms with valid values in fields and error messages where validation fails
body: <p>I have created a PHP form to take 4  text fields name, email, username and password and have set validation for these. I have my code currently validating correctly and displaying messages if the code validates or not.
  However, I would like for it to keep the correctly validated fields filled when submitted and those that failed validation to be empty with an error message detailing why.</p>
  
  <p>So far I have the following code, the main form.php:</p>
  
  <pre><code>    &lt;?php
      $self = htmlentities($_SERVER['PHP_SELF']);
      ?&gt;
      &lt;form action="&lt;?php echo $self; ?&gt;" method="post"&gt;
          &lt;fieldset&gt;
          &lt;p&gt;You must fill in every field&lt;/p&gt;
          &lt;legend&gt;Personal details&lt;/legend&gt;
              &lt;?php
              include 'personaldetails.php';
              include 'logindetails.php';
              ?&gt;
              &lt;div&gt;            
                  &lt;input type="submit" name="" value="Register" /&gt;
              &lt;/div&gt;
          &lt;/fieldset&gt;
      &lt;/form&gt;
      &lt;?php
      $firstname = validate_fname();
      $emailad = validate_email();
      $username = validate_username();
      $pword = validate_pw();
      ?&gt;
  </code></pre>
  
  <p>My functions.php code is as follows:</p>
  
  <pre><code>&lt;?php
  function validate_fname() {
      if (!empty($_POST['fname']))    {
          $form_is_submitted = true;
          $trimmed = trim($_POST['fname']);
          if  (strlen($trimmed)&lt;=150  &amp;&amp; preg_match('/\\s/', $trimmed))   {
              $fname = htmlentities($_POST['fname']);
              echo "&lt;p&gt;You entered full name: $fname&lt;/p&gt;";
          }   else    {
                  echo "&lt;p&gt;Full name must be no more than 150 characters and must contain one space.&lt;/p&gt;";
          }   }
          }
  
  function validate_email() {        
      if (!empty($_POST['email']))    {
          $form_is_submitted = true;
          $trimmed = trim($_POST['email']);
          if  (filter_var($trimmed, FILTER_VALIDATE_EMAIL))   {
              $clean['email'] = $_POST['email'];              
              $email = htmlentities($_POST['email']);
  
              echo "&lt;p&gt;You entered email: $email&lt;/p&gt;";
          }   else    {
                  echo "&lt;p&gt;Incorrect email entered!&lt;/p&gt;";
          }   }   
          }
  
  function validate_username() {
      if (!empty($_POST['uname']))        {
          $form_is_submitted = true;
          $trimmed = trim($_POST['uname']);
          if  (strlen($trimmed)&gt;=5 &amp;&amp; strlen($trimmed) &lt;=10)  {
              $uname = htmlentities($_POST['uname']);
              echo "&lt;p&gt;You entered username: $uname&lt;/p&gt;";
          }   else    {
                  echo "&lt;p&gt;Username must be of length 5-10 characters!&lt;/p&gt;";
          }   }   
      }
  
  function validate_pw()  {
      if (!empty($_POST['pw']))   {
          $form_is_submitted = true;
          $trimmed = trim($_POST['pw']);
          if  (strlen($trimmed)&gt;=8 &amp;&amp; strlen($trimmed) &lt;=10)  {           
              $pword = htmlentities($_POST['pw']);
              echo "&lt;p&gt;You entered password: $pword&lt;/p&gt;";
          }   else    {
                  echo "&lt;p&gt;Password must be of length 8-10 characters!&lt;/p&gt;";      
          }   }
      }
  ?&gt;
  </code></pre>
  
  <p>How can I ensure that when submit is pressed that it will retain valid inputs and empty invalid ones returning error messages.</p>
  
  <p>Preferably I would also like there to be an alternate else condition for initial if(!empty). I had this initially but found it would start the form with an error message.</p>
  
  <p>Lastly, how could I record the valid information into an external file to use for checking login details after signing up via this form?</p>
  
  <p>Any help is greatly appreciated. </p>