Skip to content

Instantly share code, notes, and snippets.

/index.php Secret

Created Jan 17, 2018
What would you like to do?
Mailchimp API 3 Subscribe Form -- with Interest Groups/Categories
<title>MailChimp (API v3) Sign-Up Form</title>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(document).ready(function() {
// jQuery Validation
// if valid, post data via AJAX
submitHandler: function(form) {
$.post("subscribe.php", { email: $("#email").val() }, { interests: $("#myInterests").val() },function(data)
// all fields are required
rules: {
email: {
required: true,
email: true
<div id="wrapper">
<form id="signup" class="formee" action="subscribe.php" method="post">
<legend>Sign Up</legend>
<label for="email">Email Address *</label> <input name="email" id="email" type="text" data-tooltip="please provide email" required />
<h3>Which categories are you interested in</h3>
<label class="radio-label">Mens</label>
<input type="radio" name="interests" id="myInterests" value="ABCDEFG" required>
<label class="radio-label"> Womens </label>
<input type="radio" name="interests" id="myInterests" value="HIJKLMN" required>
<label class="radio-label">Both </label>
<input type="radio" name="interests" id="myInterests" value="OPQRSTU" required>
<input class="right inputnew" type="submit" title="Send" value="Send" />
<div id="response"></div>
// Put your MailChimp API and List ID hehe
$api_key = 'MY-API-KEY';
$list_id = 'MY-LIST-ID';
// Let's start by including the MailChimp API wrapper
// Then call/use the class
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp($api_key);
$interests = $_POST['interests'];
// add the email to your list
$result = $MailChimp->post("lists/$list_id/members", [
'email_address' => $_POST['email'],
'interests' => ["$interests" => true],
'status' => 'subscribed' // single opt-in
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 "<h4>Please try again.</h4>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.