Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / Javascript
Tip/Trick

Store your form data in Google Spreadsheet

Rate me:
Please Sign up or sign in to vote.
4.81/5 (12 votes)
23 Jun 2014CPOL2 min read 158.6K   28   38
This is probably the simplest solution for storing web form data into database. We will use Google Forms and Google Spreadsheets as our 'database'.

Introduction

HTML <form> tag is available as a part of HTML from the beginning of the Internet. It is used on web pages to collect data from the users. After clicking Submit button, form data is sent to server, which usually stores them in one of the databases. While use of <form> tag is easy, server side part can be sometimes challenging to set up and operate. In this tip, I will show how we can use Google Docs Forms and Google Docs Spreadsheets as our 'database' for saving user inputs.

Using the Code

To use Google Forms and Spreadsheets as your server 'database', you will need to complete some easy steps. Google account is required to complete this task.

  1. Go to docs.google.com and create a new form. In my sample, I have created form with the following fields: Email, Firstname, LastName and Company. To keep this simple, I've set all fields to type text and without any validation rules.

    Image 1

  2. Click 'View Live Form' button and check the source of the form that is shown. As the source might not be very easy to read and understand, you will have to find a lot of information. First one is the form post URL. Search for '<form action' and mind URL in action value:
    HTML
    <form action="https://docs.google.com/forms/d/1PTIFxKDZBqKdrkGAgrsa28wus8FyP6XrMTWzabRuC18/formResponse" 
    method="POST" id="ss-form" target="_self" 
    onsubmit=""><ol style="padding-left: 0"> 
  3. Find the unique names of each field. In my case, we have four different form fields. Look for the field name and mind for <input> tags. Attribute name is the parameter that we will need. Name value start with the 'entry.' keyword. The name of the email field in my example is entry_1402836733.
    HTML
    <input type="text" name="entry.1402836733" 
    value="" class="ss-q-short" id="entry_1402836733" 
    dir="auto" aria-label="Email  " title=""> 

    You will need to find names of all the form field.

  4. This is all the information you need. Now let's make our own form in HTML file. You can put the code below into the <body> tag.
    HTML
    <input id="Email" name="Email" 
    type="text"  placeholder="Email Address">
    <input id="First" name="First" 
    type="text"  placeholder="First Name">
    <input id="Last" name="Last" 
    type="text"  placeholder="Last Name">
    <input id="Company" name="Company" 
    type="text" placeholder="Company">
    <button id="ButtonSubmit" onclick="postContactToGoogle()" 
    type="button" >Send</button> 
  5. In the final step, we will create JavaScript function that will post data to Google Form. In order to work, you need to reference jQuery.
    JavaScript
    <script>
        function postContactToGoogle() {
            var email = $('#Email').val();
            var first = $('#First').val();
            var last = $('#Last').val();
            var company = $('#Company').val();
    
                $.ajax({
                    url: "https://docs.google.com/forms/d/1PTIFxKDZBqKdrkGAgrsa28wus8FyP6XrMTWzabRuC18/formResponse",
                    data: { "entry_1402836733": email,
                    "entry_1874720748": first, "entry_2092106103":
                    last, "entry_944373055": company },
                    type: "POST",
                    dataType: "xml",
                    statusCode: {
                        0: function () {
                            window.location.replace("ThankYou.html");
                        },
                        200: function () {
                            window.location.replace("ThankYou.html");
                        }
                    }
                });
        }
    </script>
    

    Code is actually simple once we collected relevant data from Google Form source. First, I use jQuery to get user inputs and we store them in local variables. Then I initiate Ajax POST call to the URL from Google Form source. URL is called with key-value parameters. After I receive statusCode from server, I redirect to browser to another page.

Points of Interest

I found that Google Forms and Google Spreadsheets can be an excellent database foundation form small scale projects and prototypes. Doing data collection this way, it is easy to share the responses.

History

I never liked history teachers. Wink | ;)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) CENT SI d.o.o.
Slovenia Slovenia
I have strange hobby - programming.
While I am not programming I am tasting some good beers Wink | ;)

Comments and Discussions

 
GeneralRE: Store your form data in Google Spreadsheet Pin
Unka_Georgr24-Jun-14 7:19
professionalUnka_Georgr24-Jun-14 7:19 
QuestionThis is not working for me - nothing is getting saved to the google doc Pin
Member 1082256215-May-14 20:56
Member 1082256215-May-14 20:56 
AnswerRe: This is not working for me - nothing is getting saved to the google doc Pin
Dejan Mauer15-May-14 23:10
Dejan Mauer15-May-14 23:10 
GeneralRe: This is not working for me - nothing is getting saved to the google doc Pin
Member 1082256216-May-14 4:41
Member 1082256216-May-14 4:41 
GeneralRe: This is not working for me - nothing is getting saved to the google doc Pin
Member 1082256216-May-14 5:48
Member 1082256216-May-14 5:48 
Questionis it code given enough?im using a notepad to test. unsuccessfull.please help Pin
Cik Miko ZaMija22-Apr-14 0:29
Cik Miko ZaMija22-Apr-14 0:29 
AnswerRe: is it code given enough?im using a notepad to test. unsuccessfull.please help Pin
Dejan Mauer22-Apr-14 1:15
Dejan Mauer22-Apr-14 1:15 
GeneralRe: is it code given enough?im using a notepad to test. unsuccessfull.please help Pin
Member 107950294-May-14 21:28
Member 107950294-May-14 21:28 
all the data succes stored into my google spreadsheet... can you help me how to validate form?
below is my coding... but i dont know how to create validation coding using jquery or javascript? please provide me full coding for validation based on my simple form...

XML
<html>
<head>

<h1>LION PARKSON RUN 2014</h1>

<!--midat tambah START-->
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>


<script type="text/javascript">
<!--
// Form validation code will come here.

function mandatoryField()
{
var order=document.forms["add"]["Order"].value;
if (order=="" || order==null)
{
  alert("Order must be filled out");
  return false;
  }
var name=document.forms["add"]["Name"].value;
if (name=="" || name==null)
{
  alert("Name must be filled out");
  return false;
  }
  var contact=document.forms["add"]["Contact"].value;
if (contact=="" || contact==null)
{
  alert("Contact Number must be filled out");
  return false;
  }
   var identity=document.forms["add"]["Identity"].value;
if (identity=="" || identity==null)
{
  alert("Please enter your Identity card correctly without '-'");
  return false;
  }
   var citizenship=document.forms["add"]["Citizenship"].value;
if (citizenship=="" || citizenship==null)
{
  alert("Please identify your citizenship");
  return false;
  }
    var emergencycontact=document.forms["add"]["Emergencycontact"].value;
if (emergencycontact=="" || emergencycontact==null)
{
  alert("Please enter your emergency contact");
  return false;
  }
   var emergencyname=document.forms["add"]["Emergencyname"].value;
if (emergencyname=="" || emergencyname==null)
{
  alert("Please enter your emergency contact name");
  return false;
  }

var smoke = document.getElementById('smoker').checked;
var smoke = document.getElementById('nonsmoker').checked;

if((smoker1=="")&&(nonsmoker2==""))
{
alert("Select either smoker or non smoker");
}

var fitness = document.getElementById('atheletic').checked;
var fitness = document.getElementById('fit').checked;
var fitness = document.getElementById('moderate').checked;
var fitness = document.getElementById('poor').checked;

if((atheletic=="")&&(fit=="")&&(moderate=="")&&(poor==""))
{
alert("Select your fitness level");
}

var x=document.forms["add"]["Health"].value;
if (x==null || x=="")
{
  alert("Please describe details abput your health condition");
  return false;
  }
}

</script>
</head>



<body>
<form action="https://docs.google.com/forms/d/14T-xlI6pIqJoJPhYDPFfa1XC5OeAbhDtFGtvyZbNfWo/formResponse" name="add" method="POST" id="ss-form" target="_self" onSubmit="return mandatoryField()"><ol style="padding-left: 0">

<fieldset>
<legend>[Please fill the form] </legend>

<label>Order Number:</label>
<input id="Order" name="Order" type="text" class="text required" placeholder=""><br><br>

<label>Name:</label>
<input id="Name" name="Name" type="text"  placeholder=""><br><br>

<label>Contact Number:</label>
<input id="Contact" name="Contact" type="text"  placeholder=""><br><br>

<label>Identity Card/Passport Number:</label>
<input id="Identity" name="Identity" type="text"  placeholder=""><br><br>

<label>Citizenship:</label>
<input id="Citizenship" name="Citizenship" type="text" placeholder=""><br><br>

<label>Emergency Contact Number:</label>
<input id="Emergencycontact" name="Emergencycontact type="text"  placeholder=""><br><br>

<label>Emergency Contact Name:</label>
<input id="Emergencyname" name="Emergencyname type="text"  placeholder=""><br><br>

<label>Are you</label><br>
<input type="radio" name="smoke" id="smoker" value="Smoker"> Smoker
<input type="radio" name="smoke" id="nonsmoker" value="Non Smoker"> Non Smoker <br><br>

<label>How would you describe your fitness level?</label><br>
<input type="radio" name="fitness" id="atheletic" value="Atheletic"> Atheletic
<input type="radio" name="fitness" id="fit" value="Fit"> Fit
<input type="radio" name="fitness" id="moderate" value="Moderate"> Moderate
<input type="radio" name="fitness" id="poor" value="Poor"> Poor <br><br>

<label>Health Condition:</label><br>
<textarea id="Health" name="Health type="text" rows="4" cols="30"></textarea><br><br>


<label>Blood Type:</label><br>
<input type="radio" name="blood1" id="O+" value="O+"> O+
<input type="radio" name="blood1" id="O-" value="O-"> O-
<input type="radio" name="blood1" id="A+" value="A+"> A+
<input type="radio" name="blood1" id="A-+" value="A-"> A-
<input type="radio" name="blood1" id="B+" value="B+"> B+
<input type="radio" name="blood1" id="B-" value="B-"> B-
<input type="radio" name="blood1" id="AB+" value="AB+"> AB+
<input type="radio" name="blood1" id="AB-" value="AB-"> AB- <br><br>

<button id="Submit" onClick="postContactToGoogle()" type="button">Send</button>
</fieldset>
</form>


<script>
        function postContactToGoogle() {
    var order = $('#Order').val();
    var name = $('#Name').val();
    var contact = $('#Contact').val();
    var identity = $('#Identity').val();
    var citizenship = $('#Citizenship').val();
    var emergencycontact = $('#Emergencycontact').val();
    var emergencyname = $('#Emergencyname').val();
    var smoke = $('input[name="smoke"]:checked').val();
    var fitness = $('input[name="fitness"]:checked').val();
    var health = $('#Health').val();
    var blood = $('input[name="blood"]:checked').val();



                $.ajax({
                    url: "https://docs.google.com/forms/d/14T-xlI6pIqJoJPhYDPFfa1XC5OeAbhDtFGtvyZbNfWo/formResponse" ,
                    data: { "entry_142237880": order, "entry_10718042": name, "entry_961796848": contact, "entry_1770630418": identity, "entry_1580422154": citizenship, "entry_2140913417": emergencycontact, "entry_1305459562": emergencyname, "entry.1017972417": smoke, "entry.1163478879": fitness, "entry_980341489": health, "entry.855854282": blood },


                    type: "POST",
                    dataType: "xml",
                    statusCode: {
                        0: function () {
                            window.location.replace("THANKYOU.html");
                        },
                        200: function () {
                            window.location.replace("THANKYOU.html");
                        }
                    }
                });
        }
    </script>
<!--topex tambah END-->

</body>
</html>

Questionother entries which are not text Pin
Member 107269625-Apr-14 6:39
Member 107269625-Apr-14 6:39 
Questioncan we get the entry ID by code? Pin
graytay24-Mar-14 2:06
graytay24-Mar-14 2:06 
AnswerRe: can we get the entry ID by code? Pin
graytay24-Mar-14 2:39
graytay24-Mar-14 2:39 
GeneralRe: can we get the entry ID by code? Pin
Member 1144118812-Feb-15 19:42
Member 1144118812-Feb-15 19:42 
Questioncheckbox Pin
Member 1061643921-Feb-14 21:18
Member 1061643921-Feb-14 21:18 
AnswerRe: checkbox Pin
Dejan Mauer21-Feb-14 22:49
Dejan Mauer21-Feb-14 22:49 
Questionfetch from google Spreadsheet Pin
Jubayer Ahmed9-Feb-14 20:03
professionalJubayer Ahmed9-Feb-14 20:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.