-
-
Save BrookeDot/4214413 to your computer and use it in GitHub Desktop.
mailchimp ajax signup form example (without ketchup)
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> | |
</head> | |
<body> | |
<div id="email"> | |
<span>Enter your email to sign up</span> | |
<form action="/subscribe.php" id="invite" method="POST"> | |
<input type="text" placeholder="[email protected]" name="email" id="address" data-validate="validate(required, email)"/> | |
<button type="submit">»</button> | |
</form> | |
<span id="result"></span> | |
</div> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
$('#invite').submit(function() { | |
var sEmail = $('#address').val(); | |
if (validateEmail(sEmail)) { | |
var action = $(this).attr('action'); | |
$.ajax({ | |
url: action, | |
type: 'POST', | |
data: { | |
email: $('#address').attr('value') | |
}, | |
success: function(data){ | |
$('#result').html(data).css('color', 'green'); | |
}, | |
error: function() { | |
$('#result').html('Sorry, an error occurred.').css('color', 'red'); | |
} | |
}; | |
} | |
else{ | |
$('#result').html('Please enter a valid email').css('color', 'red'); | |
} | |
return false; | |
}); | |
}); | |
function validateEmail(sEmail) { | |
var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; | |
if (filter.test(sEmail)) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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 | |
$apiKey = 'your-api-key'; | |
$listId = 'your-list-id'; | |
$double_optin=false; | |
$send_welcome=false; | |
$email_type = 'html'; | |
$email = $_POST['email']; | |
//replace us2 with your actual datacenter | |
$submit_url = "http://us2.api.mailchimp.com/1.3/?method=listSubscribe"; | |
$data = array( | |
'email_address'=>$email, | |
'apikey'=>$apiKey, | |
'id' => $listId, | |
'double_optin' => $double_optin, | |
'send_welcome' => $send_welcome, | |
'email_type' => $email_type | |
); | |
$payload = json_encode($data); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $submit_url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload)); | |
$result = curl_exec($ch); | |
curl_close ($ch); | |
$data = json_decode($result); | |
if ($data->error){ | |
echo $data->error; | |
} else { | |
echo "Got it, you've been added to our email list."; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
would probably do a lot of good to do do at least a little verification of email as well. i did it with this:
$email = trim($_POST['email']);
if(filter_var($email, FILTER_VALIDATE_EMAIL)==false)
{
echo json_encode("Invalid Email"); die;
}