This tutorial shows you how to submit data to a Google Spreadsheet with a custom PHP form.
DOWNLOAD THE SOURCE CODE
A neat little feature in Google Docs is the ability to build a spreadsheet from a public HTML form. This allows you to effectively use Google Spreadsheets as databases, with the added benefit of all the formatting and reporting tools available within Google Docs.
The default Google Forms are pretty bland though. The good news is that you can recreate the form using PHP, allowing you to customise it any way you want.
The first step is to create a Google Form from within Google Docs. For this demo I have created a simple form with two inputs: favourte colour and favourite fruit. You can see the default form here.
If you take a look at the orginal Google Forms HTML, you will see that for all of the markup, the form itself is very simple. There is one HTML form that does a post back to http://spreadsheets.google.com/formResponse?formkey=dDVuRGlwQjgxSWxxUHV3dFN2TW9NdWc6MA&ifq (although this URL will obviously change for your own forms), and two forms fields: entry.0.group and entry.1.group.
To recreate this form in PHP we first add a variable to define the post back URL.
$formURL = "http://spreadsheets.google.com/formResponse?formkey=dDVuRGlwQjgxSWxxUHV3dFN2TW9NdWc6MA&ifq";
We then need a function to recreate the form elements.
function buildMultipleChoice($group, $heading, $options)
{
echo '<div>' . $heading . '</div>';
echo '<ul style="list-style-type: none;">';
foreach ($options as $option)
{
echo '<li><input type="radio" name="entry.' . $group . '.group" value="' . $option . '">';
echo $option;
echo '</li>';
}
echo '</ul>';
}
The buildMultipleChoice function will recreate the html form elements for the multiple choice questions. Given a group number (0 or 1 for this demo), a heading and a array of options, the function will output the basic HTML required to display the form question and input options.
The HTML here includes only the basics required to post the data back to Google, which is the radio input element name and the value. Using this base code you can go on to style the HTML in a more visually appealing way.
Using the code from step 2 we can now easily create the form itself.
<?php
echo '<form action="' . $formURL . '" method="POST">';
buildMultipleChoice(0, 'What is your favourite colour?', array('Red', 'Green', 'Blue'));
buildMultipleChoice(1, 'What is your favourite fruit?', array('Apple', 'Banana', 'Pear'));
echo '<input type="submit" name="submit" value="Submit">';
echo '</form>';
?>
Again this code has been kept to a minimum to show you the base code required to create the form. The end result of this code is displayed in the screenshot below.
As the form is submitted by your users, you can view the results in the Google Docs SpreadSheet associated with the form.
Here we recreated a Google Form that offered to multiple choice questions. Google Forms can be used to create more complicated forms, but they can all be recreated in PHP using the same logic that was applied here.
Go back to the Tutorial Index
Yahoo Query Language (YQL) and PHP
This free tutorial shows you how to use the YQL web service from PHP.