Skip to content

Instantly share code, notes, and snippets.

@bkmorse
Created November 19, 2013 14:55
Show Gist options
  • Select an option

  • Save bkmorse/7546549 to your computer and use it in GitHub Desktop.

Select an option

Save bkmorse/7546549 to your computer and use it in GitHub Desktop.
$(document).ready(function()
{
function displayAjaxMessage(message)
{
$("#newsletter-form-message").html(message);
$("#newsletter-form-message").show();
}
$("#newsletter-form").submit(function(event)
{
// stop the contact form from submitting normally
event.preventDefault();
// hide any left over message from a previous submit
$("#newsletter-form-message").hide();
$.ajax(
{
url: "/",
type: "post",
dataType: "html",
data: $(this).serialize(),
// there was an error
error: function(jqXHR, textStatus, errorThrown)
{
displayAjaxMessage("Sorry, there was an error joining the list, please try again.");
},
success: function(html, textStatus, jqXHR)
{
// displayAjaxMessage(html);
if (html.match(/<title>Error<\/title>/))
{
var error = $(html).find('ul li:first').text();
if (error == "Your email address is already in the mailing list")
{
displayAjaxMessage("Your email address is already on the mailing list.");
}
else if (error == "Invalid email address")
{
displayAjaxMessage("Enter a valid email address.");
}
else if (error == "You must submit an email address")
{
displayAjaxMessage("You must submit an email address.");
}
}
else
{
$("#newsletter-form").hide();
displayAjaxMessage("Thanks! You should receive a confirmation email!");
}
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment