Created
November 3, 2012 19:31
-
-
Save dhcole/4008406 to your computer and use it in GitHub Desktop.
Submit data from html form to Google Doc Spreadsheet. Uses Bootstrap components for auto-complete region list and date selection.
This file contains hidden or 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
$(function(){ | |
var formUrl = '/* ex: https://docs.google.com/a/developmentseed.org/spreadsheet/formResponse?formkey=... */'; | |
// Set up map | |
var m = mapbox.map('map').addLayer(mapbox.layer().id(' /* mapbox-account.id */ ')); | |
// Set up map ui features with point selector | |
var ui = mapbox.ui().map(m).auto().pointselector(function(d) { | |
// Remove all points except the most recent | |
for (var i = 0; i < d.length - 1; i++) { | |
var locs = ui['_pointselector'].locations(); | |
ui['_pointselector'].deleteLocation(locs[i]); | |
} | |
saveLatLon(d[0]); | |
}); | |
// Get region data from google spreadsheet and set up LGA typeahead | |
mapbox.converters.googledocs('/* spreadsheet key */', '/* sheet number */', typeAhead); | |
// Set up date pickers | |
// Uses http://www.eyecon.ro/bootstrap-datepicker | |
var now = new Date(); | |
now = now.getMonth() + '/' + now.getDate() + '/' + now.getFullYear(); | |
$('/* Selectors for date fields */').val(now).datepicker(); | |
// Handle form submission | |
$('form').submit(function(e) { | |
var button = $('input[type=submit]', this), | |
data = $(this).serialize(); | |
e.preventDefault(); | |
if (validate($(this))) { | |
button.button('loading'); | |
$.ajax({ | |
type: 'POST', | |
url: formUrl, | |
data: data, | |
complete: function() { | |
button.button('reset'); | |
// After submission, redirect to main page | |
window.location = 'index.html#new'; | |
} | |
}); | |
} | |
// All fields are required | |
function validate(form) { | |
$('.control-group').removeClass('error'); | |
$('input, textarea', form).each(function() { | |
var tag = $(this)[0].tagName.toLowerCase(), | |
type = $(this).attr('type'); | |
// Validate radio buttons | |
if (tag === 'input' && type === 'radio') { | |
var name = $(this).attr('name'); | |
if ($('[name="' + name + '"]:checked').length < 1) { | |
$(this).parent().parent().parent().addClass('error'); | |
} | |
} | |
// Validate text fields | |
if ((tag === 'input' && type === 'text') || tag === 'textarea') { | |
if ($(this).val() === '' && !$(this).parent().hasClass('radio')) { | |
$(this).parent().parent().addClass('error'); | |
} | |
} | |
}); | |
if ($('.control-group.error').length < 1) return true; | |
$('.control-group.error').length | |
$('html, body').animate({ | |
scrollTop: $('.control-group.error').offset().top - 20 | |
}, 500); | |
return false; | |
} | |
}); | |
// Use typeahead to select region | |
// Uses http://twitter.github.com/bootstrap/javascript.html#typeahead | |
function typeAhead(features) { | |
var lgas = []; | |
// Pluck `region, state` values | |
for (var i = 0; i < features.length; i++) { | |
lgas.push(features[i].properties.lgastate); | |
} | |
$('/* Element for typeahead */').typeahead({source: lgas}).change(function() { | |
var position = $.inArray($(this).val(), lgas); | |
if (position >= 0) { | |
var coords = features[position].geometry.coordinates, | |
loc = { lon: coords[0], lat: coords[1] }; | |
saveLatLon(loc); | |
m.center(loc).zoom(7); | |
$('#map-control').show(); | |
} | |
}); | |
} | |
function saveLatLon(loc) { | |
$('#entry_1').val(loc.lon); | |
$('#entry_2').val(loc.lat); | |
} | |
}); |
Excellent, I used it on my static contact form.Its save my time on writing server scripts.
Hi, dhcole! I'm new to GitHub... may I use this code anywhere? Do I need to cite you in some way? Thanks!
I was strugling with that from last few days, nothing was working. At the end I found a very good solution.
Enjoy!
http://www.nanoblogsonline.com/2018/02/how-to-submit-html-form-to-google-sheet.html
Please can somebody help me?
I have set up an order form in php, then after posting the form I have check.php file which displays the order information.
I have set up the google sheets, with the script (working fine), but how can I get the order data to be written in the google sheets?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Dhcole, thanks for your post. It was really helpful in getting me to integrate my custom forms with google sheets however, I have problems getting the textarea, radio button and text input validations to work. I cant figure out what i am doing wrong. I also noticed that each time my form is submitted and i click the google link to submit another entry, it reverts to the default google form instead of my custom form. Is there any work around this and kindly please take a look at my code below and offer some tips on getting the validations to work. thanks.