By popular demand, I updated my code and posted it as new GitHub repository that utilizes newer Mailchimp Api 3.0. Please read the tutorial below to create AJAX based MailChimp subscribe form on your PHP website with a little jQuery knowledge.
Original post using (older and no longer supported) MailChimp API 1.0 can be found here.
What this code does
- Validate field contents
- Pass the form submission to MailChimp and add the record to your list
- Basic fallback method for very, very old browser or does not have JavaScript activated
- Very customizable and easily expandable, depending on your requirements
- jQuery and jQuery Validation plugin called from CDNJS to keep number of files to maintain at minimal
Tools/Credits
- jQuery
- jQuery Validation Plugin
- MailChimp API v3 PHP Wrapper
MailChimp API Key and List ID
Below we start, we need to generate public API key from your MailChimp account and also identify your list ID. If you are not sure how to retrieve those, follow these links from MailChimp Knowledge Base to retrieve your account’s API key and find your list ID.
The Form Page (index.php)
jQuery will handle the AJAX using
$.postmethod and jQuery Validation plugin will validate the field values including email address format.
For this example, I am including first name and last name as part of the form fields. Feel free to make adjustments as required. Although keep in mind after changing the form fields, you may also need to adjust the
subscribe.phpfile too.
MailChimp (API v3) Sign-Up Form
Form Processor (subscribe.php)
I try to include as much comment as possible to make the code self explanatory.
post("lists/$list_id/members", [ 'email_address' => $_POST["email"], 'merge_fields' => ['FNAME'=>$_POST["fname"], 'LNAME'=>$_POST["lname"]], 'status' => 'subscribed', ]); if ($MailChimp->success()) { // Success message echo "Thank you, you have been added to our mailing list.
"; } else { // Display error echo $MailChimp->getLastError(); // Alternatively you can use a generic error message like: // echo "Please try again.
"; } ?>
Download
Click here to download the basic files to create MailChimp subscribe form on your website. Alternatively, you can also visit the GitHub repository.
Coming Soon Templates
The MailChimp subscribe form example only includes basic HTML page. If you are thinking of using it for your upcoming site, and you have limited resources to create the page design, check out my list of 5 Clean and Free Coming Soon Page Templates and Site Templates > Specialty Pages > Under Construction" href="http://themeforest.net/category/site-templates/specialty-pages/under-construction?ref=mayksyu" target="_blank">Coming Soon & Under Construction Templates on ThemeForest.
The post MailChimp Subscribe Form with PHP, jQuery and AJAX (API 3.0) appeared first on Michael Sunarlim.
This post first appeared on Remote Web, E-Commerce, SEO, Online Marketing & St, please read the originial post: here