Skip to content

Instantly share code, notes, and snippets.

@unixbigot
Created April 25, 2017 00:58
Show Gist options
  • Save unixbigot/37bdcbb307f1becc4b2d706cd8c8a09f to your computer and use it in GitHub Desktop.
Save unixbigot/37bdcbb307f1becc4b2d706cd8c8a09f to your computer and use it in GitHub Desktop.
Add an AWS-Lambda contact form to the "Agency" theme for Hugo (gohugo.io)
// Follow the instructions at https://www.codeengine.com/articles/process-form-aws-api-gateway-lambda/
// Including adding the custom javascript from that article (plus this file)
//
// In config.toml set postURL = "/rest/contact/"
// Support serialising the contact form as JSON to send to AWS Lambda
jQuery(document).ready(function ($) {
$('form[id=contactForm]').off('submit').on('submit', function (e) {
e.preventDefault();
var formJSON = $(this).formToJson({pretty: true, delimiter: '.'});
console.log("Processing contact form: " + formJSON);
$.ajax({
url: "/rest/contact/",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: formJSON,
cache: false,
success: function (response) {
console.log("Form success: "+response);
if (response.errorMessage) {
this.error(response.errorMessage);
return;
}
// process success message
$('form[id=contactForm] #success').hide();
$('form[id=contactForm] #error').hide();
$('form[id=contactForm] #success').show();
},
error: function (msg) {
console.log("Form error: "+response);
// process errors
$('form[id=contactForm] #success').hide();
$('form[id=contactForm] #error').hide();
$('form[id=contactForm] #error').show();
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment