пятница, 20 ноября 2009 г.

Step 1 - Create the Google Form

http://www.brighthub.com/hubfolio/matthew-casperson/articles/55171.aspx

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.

PHP Google Forms screenshot

Step 2 - The form elements

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.

Step 3 - Creating the form

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.

PHP Google Forms screenshot

Step 4 - View the results

As the form is submitted by your users, you can view the results in the Google Docs SpreadSheet associated with the form.

PHP Google Forms screenshot

Комментариев нет:

Отправить комментарий