-
-
Save anonymous/d1858a846d382482c6c27cacb6d4db93 to your computer and use it in GitHub Desktop.
Mailchimp API 3 Subscribe Form -- with Interest Groups/Categories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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