Welcome Guest, Not a member yet? Register   Sign In
JQuery AJAX success/error response repeats twice?
#1

[eluser]gwerner[/eluser]
I have a simple single text input field that I'm submitting via ajax. Everything seems to be working normal except that it outputs the success or error response twice. I've checked firebug and it looks like it's only sending the response once?

This is the code I'm using when the user submits the form. There are two methods in this controller. The one below which handles the form submission and the index method. Any ideas on why it outputs either the success or failure twice? It outputs the message in the response div located in the view like "Please enter a valid email address.Please enter a valid email address.". Any ideas or help is appreciated.


Code:
// save email subscribers
public function email()
{
$this->load->library('form_validation');

// let's make sure this is an ajax request
if ($this->input->is_ajax_request()) {
  // validate the fields
  $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');

  // if failed validation reload page and display values
  if ($this->form_validation->run() == FALSE)
  {
   $response['status'] = 'error';
   $this->output->set_output(json_encode($response));
  }
  else
  {
   // passed validation so lets insert the data
   $data = array('subscribe_email' => $this->input->post('email'));
   $this->home_model->add_email($data);
  }
}
else {
  // I'm not AJAX!
  log_message('error', 'Email Subscribe - Non-ajax request');
  $this->output->set_status_header(400);
}
}

This is the jquery.

Code:
$(document).ready(function() {
$('#emailoffers input#submit').click(function(e) {
  e.preventDefault();

  var email = $("#email").val();

  $.ajax({
   type: 'post',
   url: 'home/email',
   data: 'email='+email,
   dataType: 'json',
   success: function(results) {
    if(results.status == 'success') {
     $('div#response').hide().html("Success! Thanks for subscribing.").fadeIn(500);
    }
    else if(results.status == 'error') {
     $('div#response').hide().html("Please enter a valid email address.").fadeIn(500);
    }
   }
  }); // end ajax
});
});


Messages In This Thread
JQuery AJAX success/error response repeats twice? - by El Forum - 07-18-2012, 03:59 PM
JQuery AJAX success/error response repeats twice? - by El Forum - 07-18-2012, 04:07 PM
JQuery AJAX success/error response repeats twice? - by El Forum - 07-19-2012, 12:51 PM
JQuery AJAX success/error response repeats twice? - by El Forum - 07-19-2012, 01:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB