Skip to content

Instantly share code, notes, and snippets.

@pavenuto
Created September 21, 2010 20:09
Show Gist options
  • Save pavenuto/590435 to your computer and use it in GitHub Desktop.
Save pavenuto/590435 to your computer and use it in GitHub Desktop.
// <h3>Join Our Mailing List</h3>
//
// <div id="theForm">
// <form action="http://inmunited.createsend.com/t/r/s/tdfly/" method="post" id="subForm">
// <input type="text" class="text" name="cm-tdfly-tdfly" placeholder="Email Address" id="tdfly-tdfly" />
// <input type="submit" class="submit" value="GO" />
// </form>
// </div>
// <div id="confirmation">
// Thanks for subscribing!
// </div>
$('#subForm').ajaxSignup({
campaign_monitor_id: 'tdfly',
wp_theme_url: '/wp-content/themes/clsc'
});
$.fn.ajaxSignup = function(settings) {
var config = {
messages: {
invalidEmail: 'Please enter a valid email address',
serverFail: 'The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.'
}
};
// allow custom settings to passed in
if (settings) $.extend(config, settings);
return this.each(function(){
// cachin' some vars
var $form = $(this).parent('form'),
$form_div = $("#theForm"),
$confirmation = $('#confirmation');
action = $form.attr('action'),
id = config.campaign_monitor_id,
emailId = id + "-" + id;
$(this).bind('submit', function(e) {
e.preventDefault();
// check for email validation
if (!checkEmail(emailId)) { alert(config.messages.invalidEmail); return; }
var str = $form.serialize();
var serialized = str + "&action=" + action;
$.ajax({
url: config.wp_theme_url + "/proxy.php",
type: "POST",
data: serialized,
success: function(data){
if (data.search(/invalid/i) != -1) {
alert(config.messages.serverFail);
} else {
$form_div.hide();
$confirmation.slideDown("slow");
$confirmation.tabIndex = -1;
$confirmation.focus();
}
}
});
});
});
function checkEmail(email) {
var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var emailVal = $("#" + email).val();
return pattern.test(emailVal);
}
};
<?php
header('Content-type: text/javascript');
// Extract form action and assign to variable
$action = $_POST['action'];
// Assign form array to single variable
$arr = $_POST;
// Remove "action" key from array
unset($arr['action']);
// Loop through and add keys and values to a single variable
foreach ($arr as $key => $value) {
if (is_array($value))
{
foreach ($value as $subvalue) {
$formVars .= $key."=".urlencode($subvalue)."&";
}
}
else
{
$formVars .= $key."=".urlencode($value)."&";
}
}
// Remove last "&"
$formVars = substr($formVars,0,-1);
// Get the response
$handle = curl_init($action);
// If there is something, read and return
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $formVars);
curl_setopt ($handle, CURLOPT_FOLLOWLOCATION, 1);
echo curl_exec($handle);
curl_close($handle);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment