Last active
January 30, 2016 23:16
-
-
Save jb41/57502a178dae87332aac to your computer and use it in GitHub Desktop.
POST data to Google Spreadsheet via Sheetsu API (jQuery, simple demo)
This file contains 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
<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> |
This file contains 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
// 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; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome. Thank you!