Skip to content

Instantly share code, notes, and snippets.

@jb41
Last active January 30, 2016 23:16
Show Gist options
  • Save jb41/57502a178dae87332aac to your computer and use it in GitHub Desktop.
Save jb41/57502a178dae87332aac to your computer and use it in GitHub Desktop.
POST data to Google Spreadsheet via Sheetsu API (jQuery, simple demo)
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<form id="form">
<input type="text" name="id">
<input type="text" name="email">
<button type="submit">submit</button>
</form>
</body>
</html>
// jQuery snippet for changing HTML from into JSON
(function ($) {
$.fn.serializeFormJSON = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
})(jQuery);
$('#form').submit(function(e) {
// prevent default submiting form
e.preventDefault();
// serialize data to JSON
var data = $('#form').serializeFormJSON();
$.ajax({
url: 'YOUR_URL_TO_SHEETSU_API_HERE',
data: data,
dataType: 'json',
type: 'POST',
// place for handling successful response
// showing (redirecting to) something like /thanks.html
//page could be a good idea
success: function(data) {
console.log(data);
},
// handling error response
error: function(data) {
console.log(data);
}
});
return false;
});
@Xen0phobe
Copy link

Awesome. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment