Answers.Body: <p>Had a response from Pixel &amp; Tonic on this. Which explains the process.</p>
  
  <hr>
  
  <p>The problem you're seeing is caused by returnJson() as that also ends the process after outputting the JSON data. Most likely onSaveUser (without Before) is a better place for this code; that ensures the user has been saved to the database before running the event code.</p>
  
  <hr>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-24T11:42:33.057Z
Answers.Text: Lettie could you share your plugin please? I need to do exactly the same thing: Add a user to a mailchimp list after saving them. Thank you in advance.
Answers.UserDisplayName: Adam Menczykowski
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-24T16:29:42.140Z
Answers.Text: @adamMenczykowski it's not actually a separate plugin. I had to customise 2 other plugins that I was using to get it working. alwaysValidate and MailchimpSubscribe. So it would require a bit of coding to get it to work for you. Files are a little messy with loads of comments. But happy to ping the relevant bits to you if you think you can make use of it?
Answers.UserDisplayName: Lettie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-24T20:32:46.727Z
Answers.Text: That would be amazing, as a lesson in what you did and to help me out - I would greatly appreciate it. If you could email adammenczykowski@gmail.com please
Answers.UserDisplayName: Adam Menczykowski
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-10T14:18:17.047Z
Answers.LastActivityDate: 2015-06-10T14:18:17.047Z
Answers.UserDisplayName: Lettie
Body: <p>I have a plugin that has an onBeforeSaveUser event. This fires the mailchimpsubscribe plugin to subscribe users to selected newsletters.</p>
  
  <p>Everything works correctly the user is subscribed and then the saveUser function saves the users newsletter preferences to the database.</p>
  
  <p>I am now trying to get it to work with an ajax call, which again works correctly and subscribes the user to mailchimp. However after sending the json response it does not appear that the saveUser function is being fired as nothing is updated in the database for the selections made.</p>
  
  <p>Could the ajax be stopping the saveUser from being fired?</p>
  
  <p>Ajax:</p>
  
  <pre><code>// Get the post data
  var data = $(this).serialize();
  
  // Send it to the server
  $.post('/', data, function (response) {
  
      if (response.success) {
        $form.parents('.question-box').fadeOut(function () {
          $(this).remove();
          $('.question-box').first().fadeIn();
        });
      } else {
          var message = response.message;
        alert('Unfortunately, we were unable to save your data at this point. Please try again later.'+message);
      }
  });
  </code></pre>
  
  <p>Mailchimp Plugin:</p>
  
  <pre><code>if ($api-&gt;errorCode) { // an api error occured 
  
        // Respond appropriately to Ajax Requests.....
  
  } else { // list subscribe was successful
  
        // Respond appropriately to Ajax Requests
        if (craft()-&gt;request-&gt;isAjaxRequest())
        {
          return $this-&gt;returnJson(array(
            'success' =&gt; true,
            'errorCode' =&gt; 1,
            'values' =&gt; array(
              'email' =&gt; $email,
              'vars' =&gt; $vars
            )
          ));
        }
  
        //..... 
  }
  </code></pre>
  
  <p>Cheers</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-10T09:41:04.010Z
Comments.Text: Could you add relevant code samples (PHP/JS) to your question? Might make it easier to pin down the issue.
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-10T09:49:15.377Z
Comments.Text: I had an issue in Craft where events weren't triggered when deleting entries from the entry listing page (using the checkboxes). P&T said this was a bug in Craft they are aware of. Maybe this is related in the sense events aren't being triggered for AJAX requests?
Comments.UserDisplayName: Alec Ritson
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-10T09:56:21.273Z
Comments.Text: @mmikkel code added. As I mentioned everything works as expected until  it's submitted with ajax?
Comments.UserDisplayName: Lettie
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-10T09:58:39.983Z
Comments.Text: @AlecRitson the event is being triggered it's the saverUser after the event that isn't but only when the form is submitted by ajax. Cheers
Comments.UserDisplayName: Lettie
Tags: <plugin-development><ajax><events>
Title: ajax call breaking onBeforeSaveUser
ThreadText: # ajax call breaking onBeforeSaveUser
  
  (Asked by: username_0 on 2015-06-10)
  
  I have a plugin that has an onBeforeSaveUser event. This fires the mailchimpsubscribe plugin to subscribe users to selected newsletters.
  
  Everything works correctly the user is subscribed and then the saveUser function saves the users newsletter preferences to the database.
  
  I am now trying to get it to work with an ajax call, which again works correctly and subscribes the user to mailchimp. However after sending the json response it does not appear that the saveUser function is being fired as nothing is updated in the database for the selections made.
  
  Could the ajax be stopping the saveUser from being fired?
  
  Ajax:
  
  ```
  // Get the post data
  var data = $(this).serialize();
  
  // Send it to the server
  $.post('/', data, function (response) {
  
      if (response.success) {
        $form.parents('.question-box').fadeOut(function () {
          $(this).remove();
          $('.question-box').first().fadeIn();
        });
      } else {
          var message = response.message;
        alert('Unfortunately, we were unable to save your data at this point. Please try again later.'+message);
      }
  });
  
  ```
  
  Mailchimp Plugin:
  
  ```
  if ($api->errorCode) { // an api error occured 
  
        // Respond appropriately to Ajax Requests.....
  
  } else { // list subscribe was successful
  
        // Respond appropriately to Ajax Requests
        if (craft()->request->isAjaxRequest())
        {
          return $this->returnJson(array(
            'success' => true,
            'errorCode' => 1,
            'values' => array(
              'email' => $email,
              'vars' => $vars
            )
          ));
        }
  
        //..... 
  }
  
  ```
  
  Cheers
  
  **Tags:** plugin-development, ajax, events
  
  > **Comments:**
  > **username_1**: Could you add relevant code samples (PHP/JS) to your question? Might make it easier to pin down the issue.  
  > **username_2**: I had an issue in Craft where events weren't triggered when deleting entries from the entry listing page (using the checkboxes). P&T said this was a bug in Craft they are aware of. Maybe this is related in the sense events aren't being triggered for AJAX requests?  
  > **username_0**: @mmikkel code added. As I mentioned everything works as expected until  it's submitted with ajax?  
  > **username_0**: @username_2 the event is being triggered it's the saverUser after the event that isn't but only when the form is submitted by ajax. Cheers  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-06-10. Score: 3
  
  Had a response from Pixel & Tonic on this. Which explains the process.
  
  ---
  
  The problem you're seeing is caused by returnJson() as that also ends the process after outputting the JSON data. Most likely onSaveUser (without Before) is a better place for this code; that ensures the user has been saved to the database before running the event code.
  
  ---
  
  > **Comments:**
  > **username_3**: username_0 could you share your plugin please? I need to do exactly the same thing: Add a user to a mailchimp list after saving them. Thank you in advance.  
  > **username_0**: @adamMenczykowski it's not actually a separate plugin. I had to customise 2 other plugins that I was using to get it working. alwaysValidate and MailchimpSubscribe. So it would require a bit of coding to get it to work for you. Files are a little messy with loads of comments. But happy to ping the relevant bits to you if you think you can make use of it?  
  > **username_3**: That would be amazing, as a lesson in what you did and to help me out - I would greatly appreciate it. If you could email username_3@gmail.com please  
  
  ---
  
  