Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $('document').ready(function () {
- $('form').submit(function (e) {
- //prevent the form from submitting via the browser redirect
- e.preventDefault();
- //grab attributes and values out of the form
- var data = {
- email: $('#mc-email').val()
- };
- var endpoint = $(this).attr('action');
- //make the ajax request
- $.ajax({
- method: 'POST'
- , dataType: "json"
- , url: endpoint
- , data: data
- }).success(function (data) {
- if (data.id) {
- //successful adds will have an id attribute on the object
- alert('thanks for signing up');
- }
- else if (data.title == 'Member Exists') {
- //MC wil send back an error object with "Member Exists" as the title
- alert('thanks, but you are alredy signed up');
- }
- else {
- //something went wrong with the API call
- alert('oh no, there has been a problem');
- }
- }).error(function () {
- //the AJAX function returned a non-200, probably a server problem
- alert('oh no, there has been a problem');
- });
- });
- });
- <?php
- //fill in these values for with your own information
- $api_key = 'yes my apikey xxxx';
- $datacenter = 'us14';
- $list_id = 'listid i know';
- $email = $_POST['email'];
- $status = 'subscribed';
- if(!empty($_POST['status'])){
- $status = $_POST['status'];
- }
- $url = 'https://'.$datacenter.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members/';
- $username = 'apikey';
- $password = $api_key;
- $data = array("email_address" => $email,"status" => $status);
- $data_string = json_encode($data);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($ch, CURLOPT_USERPWD, "$username:$api_key");
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($data_string))
- );
- $result=curl_exec ($ch);
- curl_close ($ch);
- echo $result;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement