<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>

CC BY-SA 3.0
2016-02-24T11:42:33.057Z
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.
Adam Menczykowski
CC BY-SA 3.0
2016-02-24T16:29:42.140Z
@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?
Lettie
CC BY-SA 3.0
2016-02-24T20:32:46.727Z
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
Adam Menczykowski
CC BY-SA 3.0
2015-06-10T14:18:17.047Z
2015-06-10T14:18:17.047Z
Lettie
<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>

CC BY-SA 3.0
2015-06-10T09:41:04.010Z
Could you add relevant code samples (PHP/JS) to your question? Might make it easier to pin down the issue.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-06-10T09:49:15.377Z
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?
Alec Ritson
CC BY-SA 3.0
2015-06-10T09:56:21.273Z
@mmikkel code added. As I mentioned everything works as expected until  it's submitted with ajax?
Lettie
CC BY-SA 3.0
2015-06-10T09:58:39.983Z
@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
Lettie
<plugin-development><ajax><events>
ajax call breaking onBeforeSaveUser
# 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**: @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  

---

## 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 adammenczykowski@gmail.com please  

---

