title: Execute window.location redirect AFTER ajax is complete
body: <p>In summary, I have a form that is loaded via AJAX. Required fields for this form are checked via another AJAX call.  Once all required fields have been filled, a new record is added to db and the user should NOW be sent to my payment page to complete the application process.  I have tried adding <code>window.location = "http://www.paymentPage.php";</code> but no matter where I place this code, it is executing along with my ajax code.  I even tried adding a <code>die(header:location('paymentPage.php'));</code> at the end of my code in validMA.php.  I know I'm missing something simple...just can't figure it out.</p>
  
  <p>PHP code:</p>
  
  <pre><code>&lt;?php 
  session_start();
  include('header.htm');?&gt;
  &lt;/head&gt; 
  &lt;body&gt; 
  &lt;div id="container"&gt;
     &lt;div id="content"&gt;&lt;div class="content"&gt;
        &lt;h1&gt;New Member Application&lt;/h1&gt;
     &lt;form method="post" name="checkUserMA" id="checkUserMA"&gt;
      &lt;label class="clear" style="width:120px"&gt;Username/Email&lt;br&gt;&lt;span class="small"&gt;&lt;/span&gt;&lt;/label&gt;
      &lt;input type="text" name="usernameMA" id="usernameMA" class="green" style="width:300px;"/&gt;&lt;br&gt;&lt;br&gt;
      &lt;input type="submit" id="checkUserMA" class="submit" value="Submit" /&gt;
  &lt;/form&gt;
      &lt;div id="errorMA"&gt;&lt;/div&gt;
      &lt;div id="resultMA"&gt;&lt;/div&gt;
      &lt;/div&gt;&lt;/div&gt;&lt;!--end content--&gt;  
  &lt;div id="footer"&gt;
  &lt;?php include("footer.htm") ?&gt;
  &lt;!--&lt;?php include("disclaimer.htm") ?&gt;--&gt;
  &lt;/div&gt;&lt;!--end footer--&gt;
  &lt;div class="clear"&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;!--end container--&gt;
  &lt;div class="clear"&gt;&lt;/div&gt;
  &lt;/body&gt; 
  &lt;/html&gt; 
  </code></pre>
  
  <p>JS code:</p>
  
  <pre><code>$(document).ready(function() {
  
      //NEW MEMBER APPLICATION
      $('#resultMA').hide();
      $('#errorMA').hide();
      $("#checkUserMA").submit(function(event){
          event.preventDefault();
          $("#resultMA").html('');
          var values = $(this).serialize();
          $.ajax({
              url: "checkMA.php",
              type: "post",
              data: values,
              success: function(result){
                  $("#resultMA").html(result).fadeIn();
                  $('.error').hide();
                  validMA();
                  window.location = "http://184.154.174.162/~nsgpcom/memberAppPay.php";
              },
              error:function(){
                 $("#resultMA").html('There was an error.').fadeIn();
              }
          });//end ajax
      });
  function validMA(){
      $("#fullFormMA").click(function(e){
          e.preventDefault();
          $("#errorMA").html('');
          var fullForm = $(this).serialize();
          $.ajax({
              url: "validMA.php",
              type: "post",
              data: fullForm,
              success: function(result){
                  $("#errorMA").html(result).fadeIn();
              },
              error:function(){
                 $("#errorMA").html('There was an error.  Please try again.').fadeIn();
              }
          });//end 
      });
      $("errorMA").hide();
  }//end function
  </code></pre>
  
  <p>validMA.php code:</p>
  
  <pre><code>&lt;?php
  session_start();
  include('functions.php');
  connect();
  $firstName = protect($_POST['firstName']); 
  $lastName = protect($_POST['lastName']);
  $address1 = protect($_POST['address1']);
  $address2 = protect($_POST['address2']);
  $city = protect($_POST['city']);
  $state = protect(strtoupper($_POST['state']));
  $zip = protect($_POST['zip']);
  $required = array('firstName','lastName','address1','city','state','zip');
      // Loop over field names, make sure each one exists and is not empty
  $error = false;
  foreach($required as $field) {
    if (empty($_POST[$field])) {
      $error = true;
    }
  }
  if ($error){
      echo "You need to fill in all required (*) fields";
  } else {
      $sql2 = "INSERT INTO newMembers (username,firstName,lastName,address1,address2,city,state,zip,publicEmail,workPhone,privateAddress1,privateAddress2,privateCity,privateState,privateZip,homePhone,cellPhone,website,facebook,twitter,linkedIn,education,licensure,certification,statement,dateAdded) VALUES ('$username','$firstName','$lastName','$address1','$address2','$city','$state','$zip','$publicEmail','$workPhone','$privateAddress1','$privateAddress2','$privateCity','$privateState','$privateZip','$homePhone','$cellPhone','$website','$facebook','$twitter','$linkedIn','$education','$licensure','$certification','$statement','$dateAdded')";
      $result2 = mysql_query($sql2) or die(mysql_error());
  }?&gt;
  </code></pre>
  
  <p>Hoping to understand this logic.  I know I will use it again and again.</p>