Advertisement
Guest User

Untitled

a guest
Sep 1st, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. $('document').ready(function () {
  2. $('form').submit(function (e) {
  3. //prevent the form from submitting via the browser redirect
  4. e.preventDefault();
  5. //grab attributes and values out of the form
  6. var data = {
  7. email: $('#mc-email').val()
  8. };
  9. var endpoint = $(this).attr('action');
  10. //make the ajax request
  11. $.ajax({
  12. method: 'POST'
  13. , dataType: "json"
  14. , url: endpoint
  15. , data: data
  16. }).success(function (data) {
  17. if (data.id) {
  18. //successful adds will have an id attribute on the object
  19. alert('thanks for signing up');
  20. }
  21. else if (data.title == 'Member Exists') {
  22. //MC wil send back an error object with "Member Exists" as the title
  23. alert('thanks, but you are alredy signed up');
  24. }
  25. else {
  26. //something went wrong with the API call
  27. alert('oh no, there has been a problem');
  28. }
  29. }).error(function () {
  30. //the AJAX function returned a non-200, probably a server problem
  31. alert('oh no, there has been a problem');
  32. });
  33. });
  34. });
  35.  
  36. <?php
  37. //fill in these values for with your own information
  38. $api_key = 'yes my apikey xxxx';
  39. $datacenter = 'us14';
  40. $list_id = 'listid i know';
  41. $email = $_POST['email'];
  42. $status = 'subscribed';
  43. if(!empty($_POST['status'])){
  44. $status = $_POST['status'];
  45. }
  46. $url = 'https://'.$datacenter.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members/';
  47. $username = 'apikey';
  48. $password = $api_key;
  49. $data = array("email_address" => $email,"status" => $status);
  50. $data_string = json_encode($data);
  51. $ch = curl_init();
  52. curl_setopt($ch, CURLOPT_URL,$url);
  53. curl_setopt($ch, CURLOPT_POST, 1);
  54. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  56. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  57. curl_setopt($ch, CURLOPT_USERPWD, "$username:$api_key");
  58. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  59. 'Content-Type: application/json',
  60. 'Content-Length: ' . strlen($data_string))
  61. );
  62. $result=curl_exec ($ch);
  63. curl_close ($ch);
  64. echo $result;
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement