title: How to dynamically show error messages through PHP
body: <p>I am creating a PHP login script. So far I have only worked on the registration.</p>
  
  <p>My question is, how can I handle validation in PHP without refreshing the page? I want to output the feedback that the user has entered information wrongly, but I don't want to refresh the page. This is because I am using AJAX, so I want it to output on the page.</p>
  
  <p>Is this possible?</p>
  
  <p>See here, if you "sign up" without filling in any of the boxes it shows you some error messages. The problem is that it reloads the page as it does it. Is there a way to not reload the page and still show this data?</p>
  
  <p><a href="http://marmiteontoast.co.uk/fyp/login-register/test/index.php" rel="nofollow">http://marmiteontoast.co.uk/fyp/login-register/test/index.php</a></p>
  
  <p>This is an example of the if statement for just the username. This is repeated with all the other fields too:</p>
  
  <pre><code>if(isset($_POST['username'])){
      $username = mysql_real_escape_string(trim($_POST['username']));
      if(strlen($username) &gt; 3){
          // passed
          if(strlen($username) &lt; 31){
          // passed
          } else {
          $_SESSION['status']['register']['error'][] = 'The Username is greater than 30 characters.';
          }
          } else {
          $_SESSION['status']['register']['error'][] = 'The username is less than 4 characters.';
          }
          } else {
          $_SESSION['status']['register']['error'][] = 'The Username is not entered.';
          }
  </code></pre>
  
  <p>Once it passes all the validation it does:</p>
  
  <pre><code>header('Location:index.php');
  </code></pre>
  
  <p>And the errors are output on the index page by:</p>
  
  <pre><code>&lt;?php
  if(isset($_SESSION['status']['register']['error'])){
  ?&gt;
   &lt;div class="alert alert-error"&gt;
   &lt;p&gt;&lt;strong&gt;There's a problem!&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;
  &lt;?php
   foreach($_SESSION['status']['register']['error'] as $error){
   // Outputs list of all errors, breaks to new line
   echo $error . '&lt;br /&gt;';
  }
  ?&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p><strong>1. Is it possible to output these dynamically with PHP?</strong></p>
  
  <p><strong>2. Could I do the validation on the front end, then just pass it to the PHP to pass to the database?</strong></p>
  
  <p><strong>2a. How would I handle running a <em>username exists</em> check if I do it front end?</strong></p>