简体   繁体   中英

More API examples needed for mailchimp - send data then redirect

I'm relatively new to mailchimp and have been using this post to implement a very simple email capture form that will redirect to a new landing page on submit. I have been using this post: using an existing form to send data to mailchimp, then redirect

I understand everything in the post except I need an example for this statement:

"However, if you want to redirect, you'd simply alter your jQuery ajax function to do so."

Can someone provide an example of how to alter the jQuery ajax function? Also I don't need it to return a success or failure message, I just need it to capture the name / email and then immediately redirect to the next page.

Thanks

Sparky is saying that you can modify the success function of the AJAX that is part of the submit function tied to form $('#signup') .

ie, Currently the success function is:

...
success: function(msg) {
        $('#message').html(msg);
    }
...

And you could just update to redirect doing something like:

...
success: function(msg) {
        document.location = '/target/path/file.html';
    }
...

EDIT - Doing the above will redirect the browser - so long as the AJAX call is successful (not necessarily that the API call was successful.) So even if the PHP is returning an error in the message - the success function is called because it is referring to the success of the AJAX call itself.

If you want to redirect only on a successful message returned by the PHP file then you'll have to check the incoming msg and handle accordingly.

...
success: function(msg) {
  if (msg.indexOf('Success') != -1) {
   document.location = '/target/path/file.html';
  } else {
   $('#message').html(msg);
  }
}
...

This will redirect the browser on a successful message return from the PHP file, otherwise it will display the message in $('#message') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM