title: How do I throw an error message within AJAX?
body: <p>For some reason I can't throw an error message to say whether or not an email exists inside of my user table. I understand that because AJAX is async I can't use try and catch error messages inside the complete function. But I tried splitting it into functions and it still doesn't work.</p>
  
  <p><strong>Try, Catch Function (I do call this else where in my code)</strong></p>
  
  <pre><code>try {
  
      // Check fields are not empty
      if (!firstName || !lastName || !aquinasEmail || !sPassword || !sCPassword || !Gender) {
          throw "One or more field(s) have been left empty.";
      }
  
      // Check the email format is '@aquinas.ac.uk'
      if(!emailCheck.test(aquinasEmail)) {
          throw "The email address you entered has an incorrect email prefix. ";
      }
  
      // Check there are not any numbers in the First or Last name
      if (!regx.test(firstName) || !regx.test(lastName)) {
          throw "First Name or Last Name is invalid.";
      }
  
      // Check the confirmation password is the same as the first password
      if (sPassword != sCPassword) {
          throw "The two passwords you've entered are different.";
      }
  
      if(duplicatedEmail()) {
          throw "Sadly, your desired email is taken. If you have forgotten your password please, &lt;a href=\"#\"&gt;Click Here&lt;/a&gt;";
      }
  
  
  
  } catch(err) {
      if (!error) {
          $('body').prepend("&lt;div class=\"error alert\"&gt;"+err+"&lt;/div&gt;");
          $('.signupInput.sPassword').val('');
          $('.signupInput.sCPassword').val('');
          setTimeout(function() {
              $('.error.alert').fadeOut('1000', function() {$('.error.alert').remove();});
          }, 2600);
      }
  
      event.preventDefault();
  }
  </code></pre>
  
  <p><strong>AJAX Function:</strong></p>
  
  <pre><code>function duplicatedEmail() {
  // Use AJAX function to do verification checks which can not be done via jQuery.
          $.ajax({
              type: "POST",
              url: "/login/ajaxfunc/verifyReg.php",
              dataType: "JSON",
              async: false,
              data: $('.signupForm').serialize(),
              success: function(data) {
                      if (data.emailTaken == true) {
                          return true;
                      } else {
                          return false;
                      }
              }
          });
  }
  </code></pre>
  
  <p><strong>verifyReg.php</strong></p>
  
  <pre><code>&lt;?php
      header('Content-Type: application/json', true);
      $error = array();
      require_once '../global.php';
  
      $_POST['aquinas-email'] = "aq142647@aquinas.ac.uk";
  
      // Check if an email already exists.
      $checkEmails = $db-&gt;query("SELECT * FROM users WHERE aquinasEmail = '{$_POST['aquinas-email']}'");
      if ($db-&gt;num($checkEmails) &gt; 0) {
          $error['emailTaken'] = true;
      } else {
          $error['emailTaken'] = false;
      }
  
      echo json_encode($error);
  ?&gt;
  </code></pre>