title: Why isn't my jQuery .ajaxForm code working?
body: <p>I'm hoping someone can help me...</p>
  
  <p>Basically, neither <code>if (data.status=='error') {</code> or <code>} else if (data.status=='success') {</code> appear to be getting called yet the data is being inserted into the database fine.</p>
  
  <p>add_alarm_code.php;</p>
  
  <pre><code>&lt;?php
  
  require_once('../inc/config.php');
  
  $con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
  
  // define and escape each POST as a variable
  
  foreach ($_POST as $key =&gt; $value) {
  
  if (!empty($value)) {
  
  ${$key} = mysqli_real_escape_string($con, $value);
  
  }
  }
  
  // insert data into database
  
  $sql="INSERT INTO alarm_code (
  alarm_code_property,
  alarm_code_code,
  alarm_code_notes,
  alarm_code_date_added
  )
  VALUES (
  '$alarm_code_property',
  '$alarm_code_code',
  '$alarm_code_notes',
  '" . date('Y-m-d') . "'
  )";
  
  if (!mysqli_query($con,$sql)) {
  echo '{ "status": "error", "message": "Error Inserting Alarm Code Data into Database: ' . mysqli_error($con) . '" }';
  } else {
  echo '{ "status": "success", "message": "The alarm code has successfully been added to the system. The page will now refresh." }';
  }
  
  mysqli_close($con);
  
  ?&gt;
  </code></pre>
  
  <p>jQuery on view_property.php;</p>
  
  <pre><code>// add alarm code submit form
  $('#add_alarm_code').ajaxForm({ 
  dataType: 'json',
  success: processJson,
  error: function (xmlRequest, textStatus, errorThrown) {
  alert(errorThrown);
  }
  });
  
  function processJson(data) { 
  
  if (data.status=='error') {
  
  alert('Error 1');
  $('#ajax_form_status_alarm_code').removeClass('green_background')
  $('#ajax_form_status_alarm_code').addClass('red_background')
  $('#ajax_form_status_alarm_code').html(data.message);
  alert('Error 2');
  
  } else if (data.status=='success') {
  
  $('#add_alarm_code').clearForm();
  $('#ajax_form_status_alarm_code').removeClass('red_background')
  $('#ajax_form_status_alarm_code').addClass('green_background')
  $('#ajax_form_status_alarm_code').html(data.message);
  
  setTimeout(function() {
  
  var currentURL = window.location.href;
  var currentHashValue = window.location.hash;
  
  if (currentHashValue!='') {
  
      location.reload(true);
  
  } else {
  
      window.location.href += "#keys";
      location.reload(true);
  
  }
  }, 3000);
  
  }
  }
  </code></pre>
  
  <p>form on view_property.php (the form is in a hidden div as it loads in a fancybox/lightbox)</p>
  
  <pre><code>echo '&lt;div style="display:none"&gt;';
  
  echo '&lt;form id="add_alarm_code" action="' . SITE_AJAX . 'add_alarm_code.php" method="post"&gt;';
  
  echo '&lt;input type="hidden" id="alarm_code_property" name="alarm_code_property" value="' . $property_id . '"&gt;';
  
  echo '&lt;fieldset&gt;&lt;legend&gt;Add an Alarm Code&lt;/legend&gt;
  &lt;table class="nobord"&gt;';
  
  echo '&lt;tr&gt;&lt;td&gt;&lt;label for="alarm_code_code"&gt;Alarm Code:&lt;/label&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" id="alarm_code_code" name="alarm_code_code" maxlength="10" required&gt;&lt;/td&gt;&lt;/tr&gt;';
  
  echo '&lt;tr&gt;&lt;td&gt;&lt;label for="alarm_code_notes"&gt;Notes:&lt;/label&gt;&lt;/td&gt;
  &lt;td&gt;&lt;textarea id="alarm_code_notes" name="alarm_code_notes" rows="6" cols="35"&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;';
  
  echo '&lt;tr&gt;&lt;td colspan="2"&gt;&lt;input type="submit" value="Save"&gt;&lt;/td&gt;&lt;/tr&gt;';
  
  echo '&lt;/table&gt;';
  
  echo '&lt;div id="ajax_form_status_alarm_code" class="ajax_form_status"&gt;&lt;/div&gt;';
  
  echo '&lt;/fieldset&gt;&lt;/form&gt;';
  
  echo '&lt;/div&gt;';
  </code></pre>
  
  <p>entire jQuery on view_property.php (in case it helps find a bug!);</p>
  
  <pre><code>&lt;script&gt;
  $(document).ready(function() {
  
  // tab functionality on load
  $(function(e) {
  
  // define variables
  var currentAttrValue = window.location.hash;
  
  if (currentAttrValue!='') {
  
  // show/hide tabs
  jQuery('.tabs ' + currentAttrValue).fadeIn(400).siblings().hide();
  
  // change/remove current tab to active
  jQuery(currentAttrValue).parent('li').addClass('active').siblings().removeClass('active');
  
  e.preventDefault();
  
  }
  
  });
  
  // tab functionality on click
  jQuery('.tabs .tab-links a').on('click', function(e)  {
  var currentAttrValue = jQuery(this).attr('href');
  
  // show/hide tabs
  jQuery('.tabs ' + currentAttrValue).fadeIn(400).siblings().hide();
  
  // change/remove current tab to active
  jQuery(this).parent('li').addClass('active').siblings().removeClass('active');
  
  e.preventDefault();
  
  });
  
  // fancybox
  $(".fancybox").fancybox({
  closeBtn        : true,
  modal           : false
  });
  
  // apply validation
  $( "#add_certificate" ).validate({
  errorClass: "error-class",
  });
  $( "#add_keys" ).validate({
  errorClass: "error-class",
  });
  $( "#add_alarm_code" ).validate({
  errorClass: "error-class",
  });
  
  // add certificate submit form
  $('#add_certificate').ajaxForm({ 
  dataType: 'json',
  success: processJson,
  });
  
  function processJson(data) { 
  
  if (data.status=='error') {
  
  $('#ajax_form_status_certificate').removeClass('green_background')
  $('#ajax_form_status_certificate').addClass('red_background')
  $('#ajax_form_status_certificate').html(data.message);
  
  } else if (data.status=='success') {
  
  $('#add_certificate').clearForm();
  $('#ajax_form_status_certificate').removeClass('red_background')
  $('#ajax_form_status_certificate').addClass('green_background')
  $('#ajax_form_status_certificate').html(data.message);
  
  setTimeout(function(){
  
  var currentURL = window.location.href;
  var currentHashValue = window.location.hash;
  
  if (currentHashValue!='') {
  
      location.reload(true);
  
  } else {
  
      window.location.href += "#certificates";
      location.reload(true);
  
  }
  }, 3000); 
  }
  }
  
  $( "#add_1_year" ).click(function() {
  
  if ($('#certificate_start_date').val()!='') {
  
  var expiry_date = moment($('#certificate_start_date').val()).add(1, 'year').format("YYYY-MM-DD");
  $('#certificate_expiry_date').val(expiry_date);
  
  } else {
  
  alert('Please enter the certificate start date.');
  
  }
  
  });
  
  // add alarm code submit form
  $('#add_alarm_code').ajaxForm({ 
  dataType: 'json',
  success: processJson,
  error: function (xmlRequest, textStatus, errorThrown) {
  alert(errorThrown);
  }
  });
  
  function processJson(data) { 
  
  if (data.status=='error') {
  
  alert('Error 1');
  $('#ajax_form_status_alarm_code').removeClass('green_background')
  $('#ajax_form_status_alarm_code').addClass('red_background')
  $('#ajax_form_status_alarm_code').html(data.message);
  alert('Error 2');
  
  } else if (data.status=='success') {
  
  $('#add_alarm_code').clearForm();
  $('#ajax_form_status_alarm_code').removeClass('red_background')
  $('#ajax_form_status_alarm_code').addClass('green_background')
  $('#ajax_form_status_alarm_code').html(data.message);
  
  setTimeout(function() {
  
  var currentURL = window.location.href;
  var currentHashValue = window.location.hash;
  
  if (currentHashValue!='') {
  
      location.reload(true);
  
  } else {
  
      window.location.href += "#keys";
      location.reload(true);
  
  }
  }, 3000);
  
  }
  }
  
  // add keys submit form
  $('#add_keys').ajaxForm({ 
  dataType: 'json',
  success: processJson,
  });
  
  function processJson(data) { 
  
  if (data.status=='error') {
  
  $('#ajax_form_status_keys').removeClass('green_background')
  $('#ajax_form_status_keys').addClass('red_background')
  $('#ajax_form_status_keys').html(data.message);
  
  } else if (data.status=='success') {
  
  $('#add_keys').clearForm();
  $('#ajax_form_status_keys').removeClass('red_background')
  $('#ajax_form_status_keys').addClass('green_background')
  $('#ajax_form_status_keys').html(data.message);
  
  setTimeout(function(){
  
  var currentURL = window.location.href;
  var currentHashValue = window.location.hash;
  
  if (currentHashValue!='') {
  
      location.reload(true);
  
  } else {
  
      window.location.href += "#keys";
      location.reload(true);
  
  }
  }, 3000);
  
  }
  }
  
  }); 
  &lt;/script&gt;
  </code></pre>