Populate Forms Which Have Been Disconnected from Google Docs

Due to popular demand, we're going to look at pre-populating Google forms using PHP. Last week's post showed how forms can be liberated from Google Docs. Since they're disconnected, we won't be able to depend on the standard method provided by Google. We're responsible for developing our own solution. Luckily, this is fairly straight forward with server-side languages like PHP.

Background

In the previous post (Disconnect Forms from Google Docs for Complete Customization), we were looking for greater control over the design of Google forms. Embedding them within our website provides some control, but little can be done with how the form itself appears. That is where disconnecting forms from Google Docs comes in handy. A downside, however, is that we'll need to build our own option for pre-populating the forms.

Pre-populating Forms

We could use plain old HTML to pre-populate the forms. If one asked visitors to enter the country they currently reside in, a default value could be assigned to the field. If "USA" is the most common choice, the field can be set as follows:

<div><label for="entry_9873420934">Country*</label> <input type="text" name="entry.9873420934" value="USA" id="entry_9873420934" /></div>

However, that doesn't quite work so well with pre-populating someone's name. Unless, of course, your organization only hires people named Franny. That's where PHP could be utilized to connect with a customer database. Let's say the following array was generated from the database:

<?php
//CREATE USER ARRAY
$userInformation = array(
     1 => array(
          'name'=>'Big Timber',
          'email'=>'btrocks@jakebible.com'
     ),
     2 => array(
          'name'=>'Kilroy 2.0',
          'email'=>'kilroywashere@jchutchins.net'
     ),
     3 => array(
          'name'=>'John Tweedy',
          'email'=>'mamasboy@scottsigler.com'
     )
);
?>

Note that the multidimensional associative array is indexed with the user IDs. To simulate the log-in process, we'll pass an ID using a GET variable which will be discussed later.

Next, we'll create some variables for holding the logged-in user information and test if an ID was passed.

<?php
//INITIALIZE VARIABLES
$userName = '';
$userEmail = '';
 
//IF USER ID WAS PASSED
if(isset($_GET['id'])) {
     //process the ID
}
?>

If an ID was found, we'll make sure it's a number and points to a user record. We can then store the corresponding user information.

<?php
//INITIALIZE VARIABLES
$userName = '';
$userEmail = '';
 
//IF USER ID WAS PASSED
if(isset($_GET['id'])) {
     //IF ID IS A NUMBER --AND-- APPEARS IN THE USER ARRAY
     if(ctype_digit((string)$_GET['id']) && array_key_exists($_GET['id'], $userInformation)) {
          $userName = $userInformation[$_GET['id']]['name'];
          $userEmail = $userInformation[$_GET['id']]['email'];
     }

}
?>

All that's left is to incorporate the variables into the form.

<form action="https://docs.google.com/__your_form_processing_address_here___" method="post" id="ss-form">
<div><label for="entry_1873320168">Name*</label> <input type="text" name="entry.1873320168" value="<?php print $userName; ?>" id="entry_1873320168" /></div>
<div><label for="entry_941720192">E-mail*</label> <input type="text" name="entry.941720192" value="<?php print $userEmail; ?>" id="entry_941720192" /></div>
<div><input type="submit" name="submit" value="Submit" id="ss-submit" /></div>
</form>

Final Code

The following code is missing some essential tags (like <body>). But it should pre-populate the form if saved to a PHP file and uploaded to a server which supports PHP. Once uploaded, the code can be activated by visiting the page with an "id" GET variable (Ex: http://www.yourwebsite.com/form.php?id=2).

Note: the form code will not submit unless you replace it with your own Google form code.

<?php
//CREATE USER ARRAY
$userInformation = array(
     1 => array(
          'name'=>'Big Timber',
          'email'=>'btrocks@jakebible.com'
     ),
     2 => array(
          'name'=>'Kilroy 2.0',
          'email'=>'kilroywashere@jchutchins.net'
     ),
     3 => array(
          'name'=>'John Tweedy',
          'email'=>'mamasboy@scottsigler.com'
     )
);
 
//INITIALIZE VARIABLES
$userName = '';
$userEmail = '';
 
//IF USER ID WAS PASSED
if(isset($_GET['id'])) {
     //IF ID IS A NUMBER --AND-- APPEARS IN THE USER ARRAY
     if(ctype_digit((string)$_GET['id']) && array_key_exists($_GET['id'], $userInformation)) {
          $userName = $userInformation[$_GET['id']]['name'];
          $userEmail = $userInformation[$_GET['id']]['email'];
     }
}
 
?>
<form action="https://docs.google.com/__your_form_processing_address_here___" method="post" id="ss-form">
<div><label for="entry_1873320168">Name*</label> <input type="text" name="entry.1873320168" value="<?php print $userName; ?>" id="entry_1873320168" /></div>
<div><label for="entry_941720192">E-mail*</label> <input type="text" name="entry.941720192" value="<?php print $userEmail; ?>" id="entry_941720192" /></div>
<div><input type="submit" name="submit" value="Submit" id="ss-submit" /></div>
</form>

Conclusion

Google Docs provides a powerful option for developing forms quickly and cheaply. The lack of customization options, however, is less than desirable. Performing a few extra steps to liberate the form lets us modify the form as needed while still being able to pre-populate forms.

One thing which still needs to be addressed is that Google forms allow us to mark certain fields as required. If the form is submitted without the fields being completed, the visitor doesn't get sent back to the customized form—they see Google's version. JavaScript could be employed to check the information before being sent, but that will have to be a topic for another day.

Related Posts

0 Comments

There are currently no comments.

Leave a Comment