4

Currently I use @skube method to submit mailchimp form. I can successfully setup and its really work. But I want to get the mailchimp error message in my site.

Like: While someone already subscribe or fake email provided, or invalid email etc that mailchimp provide.

Here is the code that @skube provided.

html code:

<form class="myform" action="http://XXXXXXXXXlist-manage2.com/subscribe/post" method="POST">
  <input type="hidden" name="u" value="XXXXXXXXXXXXXXXX">
  <input type="hidden" name="id" value="XXXXXXXXX">
  <input class="input" type="text" value="" name="MERGE1" placeholder="First Name" required>
  <input type="submit" value="Send" name="submit" id="mc-embedded-subscribe">
</form>

ajax code

$('.myform').submit(function(e) {
  var $this = $(this);
  $.ajax({
      type: "GET", // GET & url for json slightly different
      url: "http://XXXXXXXX.list-manage2.com/subscribe/post-json?c=?",
      data: $this.serialize(),
      dataType    : 'json',
      contentType: "application/json; charset=utf-8",
      error       : function(err) { alert("Could not connect to the registration server."); },
      success     : function(data) {
          if (data.result != "success") {
              // Something went wrong, parse data.msg string and display message
          } else {
              // It worked, so hide form and display thank-you message.
          }
      }
  });
  return false;
});

I searched about this but didn't find any valid documentation. I find a a github code but I can't make it work. Here is the github link

Hope someone help me.

Note: I tried using old api code method that exists but mailchimp suggest to not use that. They already release 3.0 version of API

Community
  • 1
  • 1
I Am Stack
  • 231
  • 3
  • 18

2 Answers2

2

Sorry and Thanks to Everyone for your time. I got the solution. The solution is really simple !

Need to use data['msg'] at error section.

so code will look like:

$('.myform').submit(function(e) {
  var $this = $(this);
  $.ajax({
      type: "GET", // GET & url for json slightly different
      url: "http://XXXXXXXX.list-manage2.com/subscribe/post-json?c=?",
      data: $this.serialize(),
      dataType    : 'json',
      contentType: "application/json; charset=utf-8",
      error       : function(err) { alert("Could not connect to the registration server."); },
      success     : function(data) {
          if (data.result != "success") {
              // Something went wrong, parse data.msg string and display message

              alert(data['msg']);

          } else {
              // It worked, so hide form and display thank-you message.

              alert('Thanks for subscribe');
          }
      }
  });
  return false;
});

So you may use data['msg'] in a div or p or anywhere .. where you want to show output.

Example:

$('#error').html(data['msg']);

Thanks Everyone.

I Am Stack
  • 231
  • 3
  • 18
0

Thanks for the autoresponse!

Works for me, but I have to make some changes: the form:

<form id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate myform" validate>

, without action and method.

The ajax call:

$.ajax({
              type: "post",
              url: "xxxxxx/subscribe/post-json?c=?",
              data: $this.serialize(),
              mode: 'no-cors',
              dataType    : 'json',
              contentType: "application/json",
              error       : function(err) { alert("Could not connect to the registration server."); },
              success     : function(data) {
                  if (data.result != "success") {
                      // Something went wrong, parse data.msg string and display message
alert(data['msg']);
                  } else {
                      
alert("thanks!");
                  }
              }
          });