title: AJAX Form will only either show success message OR post data to database not both
body: <p>I am using Codeigniter as my framework and have a simple contact form. This uses the form helper and i have used AJAX and a fallback in the controller if AJAX is not present.</p>
  
  <p>At the moment, my code with only either show the success message from the ajax form OR post the data to the database depending on if i change them around in the controller - my error messages work fine.</p>
  
  <p>I am confused to how it will not both post and show success message - i think i may be missing something in my controller or AJAX request?</p>
  
  <p>Here is my code as a guidance and if anyone can spot anything that would be great as it's getting on my nerves now!</p>
  
  <p>*The code i am posting now lets the data be posted into the database. When i move the post data elements below this -> <code>return $this-&gt;output-&gt;set_output(json_encode($respond)); It doesn't post to the database but shows the success message and vice versa.</code></p>
  
  <p>CONTROLLER,</p>
  
  <p>
  </p>
  
  <p></p>
  
  <pre><code>// if ajax request
          if($this-&gt;input-&gt;is_ajax_request()) {   
              $respond = array();
              if($this-&gt;form_validation-&gt;run() == FALSE) {
                  $respond['result'] = 'false';
  
                  $respond['error_message'] = $error_message;
                  $respond['errors'] = validation_errors();
  
                  // set individual errors - for warning classes
                  $respond['first_name_error'] = form_error('first_name');
                  $respond['country_error'] = form_error('country');
                  $respond['email_error'] = form_error('email');
                  $respond['message_error'] = form_error('message');
  
              } else {
  
                  $respond['result'] = 'true';
                  $respond['success_message'] = $success_message; 
  
                  // add contact message to the database
                  $this-&gt;contact_model-&gt;insert_contact_message($curr_lang, $this-&gt;input-&gt;post('first_name'), $this-&gt;input-&gt;post('country'), $this-&gt;input-&gt;post('email'), $this-&gt;input-&gt;post('phone'), $this-&gt;input-&gt;post('message'));
              }
              return $this-&gt;output-&gt;set_output(json_encode($respond));
  
          } else {
              // if ajax request failed - use CI
              if($this-&gt;form_validation-&gt;run() == FALSE) {
  
                  $data['error_message'] =  $error_message;
                  $data['errors'] = validation_errors();
              } else {
                  // add contact message to the database
                  $this-&gt;contact_model-&gt;insert_contact_message($curr_lang, $this-&gt;input-&gt;post('first_name'), $this-&gt;input-&gt;post('country'), $this-&gt;input-&gt;post('email'), $this-&gt;input-&gt;post('phone'), $this-&gt;input-&gt;post('message'));
  
                  $data['success_message'] = $success_message;
  
              }
          }
  
          // set field labels
          $data['first_name'] = $first_name;
          $data['country'] = $country;
          $data['email'] = $email;
          $data['phone'] = $phone;
          $data['message'] = $message;
  
          // initialize view name
          $data['content'] = $page;
  
          // load the view
          $this-&gt;load-&gt;view('template', $data);
      } 
  </code></pre>
  
  <p>AJAX</p>
  
  <pre><code>$('#submit').click(function(e) {
          e.preventDefault();
          // send the form data to the controller
          $.ajax({
              url: $(this).attr('action'),
              type: 'POST',
              data: $('form').serialize(),
              dataType: 'json',
              success: function(respond) {
                  if(respond.result === 'false'){
                      // function to add warning class
                      function add_error(response, field){
                          if(response){
                              $(field).addClass('warning');
                          }
                      }
  
                      // add warning classes - doing this individually as some inputs have more than one error message
                      add_error(respond.first_name_error, 'input[name="first_name"]');
                      add_error(respond.country_error, 'input[name="country"]');
                      add_error(respond.email_error, 'input[name="email"]');
                      add_error(respond.message_error, 'textarea');
  
                      // post all errors to the view
                      var error_msg = respond.error_message + respond.errors;
                      $('#error_message').html(error_msg);    
  
                  }
                  if(respond.result === 'true'){
                      // empty the form
                      $('#error_message').empty();
                      $('form').find("input[type=text], textarea").val('');
  
                      // set the success message
                      var success_msg = respond.success_message;
                      $('#success_message').html(success_msg).fadeOut(6000);                  
                  }
              }     
          });
          return false;
      });
  </code></pre>