Skip to content

Instantly share code, notes, and snippets.

/index.php Secret

Created January 17, 2018 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d1858a846d382482c6c27cacb6d4db93 to your computer and use it in GitHub Desktop.
Save anonymous/d1858a846d382482c6c27cacb6d4db93 to your computer and use it in GitHub Desktop.
Mailchimp API 3 Subscribe Form -- with Interest Groups/Categories
<html>
<head>
<title>MailChimp (API v3) Sign-Up Form</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// jQuery Validation
$("#signup").validate({
// if valid, post data via AJAX
submitHandler: function(form) {
$.post("subscribe.php", { email: $("#email").val() }, { interests: $("#myInterests").val() },function(data)
{
$('#response').html(data);
});
},
// all fields are required
rules: {
email: {
required: true,
email: true
}
}
});
});
</script>
</head>
<body>
<div id="wrapper">
<form id="signup" class="formee" action="subscribe.php" method="post">
<fieldset>
<legend>Sign Up</legend>
<div>
<label for="email">Email Address *</label> <input name="email" id="email" type="text" data-tooltip="please provide email" required />
</div>
<div>
<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>
</div>
<div>
<input class="right inputnew" type="submit" title="Send" value="Send" />
</div>
</fieldset>
</form>
<div id="response"></div>
</div>
</body>
</html>
<?php
// 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
include('./inc/MailChimp.php');
// 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