title: Using the header function in PHP to reload a page through AJAX
body: <p>I have built a register form page. It allows you to register an account and stores it in the database.</p>
  
  <p>Originally, it posted back any errors to the same page (was called index.php)</p>
  
  <p>However, before leaping into the registration page I wanted to present the user with an "I already have an account" button and "make new account" button. This is because the site is for children and I didn't want to have loads of text on the same page.</p>
  
  <p>So if you go here you'll see there are two options. These load in the relevant form via AJAX into the div login-register-wrapper. Click one and it loads register-form.php and the other is login-form.php (currently not working)</p>
  
  <p><a href="http://marmiteontoast.co.uk/fyp/login-register/index.php" rel="nofollow">http://marmiteontoast.co.uk/fyp/login-register/index.php</a></p>
  
  <p>The issue I have is that when validating, it posts back to <code>index.php</code></p>
  
  <pre><code>header('Location: index.php');
  </code></pre>
  
  <p>So if you don't put in a username, instead of posting the error out it takes you back to the first page...</p>
  
  <p>How can I make this work with AJAX so that it posts it out on my register-form.php file?</p>
  
  <p>Here is the full register page code:</p>
  
  <pre><code>&lt;?php
      session_start();
      require 'functions.php';
  
      if(isset($_POST['sign-up'])){
  
          // username
          if (isset($_POST['username'])){
              $username = mysql_real_escape_string(trim($_POST['username']));
              $_SESSION['status']['register']['username'] = $username;
  
              if(strlen($username) &gt; 3){
                      if(strlen($username) &lt; 31){
                          if(user_exists($username) === true){
                              $_SESSION['status']['register']['error'][] = 'That username is already taken. Sorry, please try again with a different username.';
                          } else{
                              // passed
                              // continue
                          }
                      } else {
                          $_SESSION['status']['register']['error'][] = 'The username is greater than 30 characters.';
                      }
                  } else {
                      $_SESSION['status']['register']['error'][] = 'The username is less than 3 characters.';
                  }
          } else {
              $_SESSION['status']['register']['error'][] = 'The username is not entered.';
          }
  
          if (isset($_POST['password'])){
              $password = mysql_real_escape_string(trim($_POST['password']));
  
              if(strlen($password) &gt;= 8){
                      $password = hash_function($password);
                  } else {
                      $_SESSION['status']['register']['error'][] = "Your secret password is too short. You should make a password with at least 8 letters.";
                  }
  
          } else {
              $_SESSION['status']['register']['error'][] = "You haven't put in a password.";
          }
  
          // Email address
          if (!empty($_POST['email_address'])){
              $email_address = mysql_real_escape_string(trim($_POST['email_address']));
              $_SESSION['status']['register']['email_address'] = $email_address;
              if(strlen($email_address) &gt; 10){ // email address less than 10
                      if(strlen($email_address) &lt; 161){ // if longer than 160
  
                          if(email_valid($email_address) == false){ // email address invalid format
                                  $_SESSION['status']['register']['error'][] = "The email address has been put in wrong. Please check and try again.";
                              }
                              else{
                                  // passed min length, passed max length, passed validation
                                  // Continue
                              }
                      }
                      else 
                      {
                          $_SESSION['status']['register']['error'][] = 'The email address is too long.';
                      }
                  } 
                  else
                  {
                      $_SESSION['status']['register']['error'][] = "The email address is too short. It can't be shorter than 10 letters.";
                  }
          }
          else{// passed (no email input)
          }
  
          if (isset($_POST['tos'])){
              $_SESSION['status']['register']['tos'] = $_POST['tos'];
              if(empty($_SESSION['status']['register']['error'])){
                  if(register($email_address, $username, $password) === true){
  
                      // Success!!
                      $_SESSION['status']['register']['success'] = true;
  
                      // Sends an email
  
                      send_email($email_address);
  
                  } else {
                      echo mysql_error();
                      die();
                      $_SESSION['status']['register']['error'][] = "Something went wrong. We're sorry. Please try again.";
                  }
              } else {}
          } else {
              $_SESSION['status']['register']['error'][] = "You have to agree to the House Rules to be able to sign up.";
          }
  
          header('Location: index.php');
      } else {
          header('Location: index.php');
      }
  
      ?&gt;
  </code></pre>