What's new

Closed Using a google drive spreadsheet as backend for a php form

Status
Not open for further replies.

Zerobyte

Forum Guru
Elite
Joined
Feb 9, 2017
Posts
3,383
Solutions
32
Reaction
2,797
Points
1,157
You do not have permission to view the full content of this post. Log in or register now. (formerly Google Docs) documents are a great way to collaborate with others to interact with your shared data online. While it lacks some of the advanced features that a SQL database has, the quick and easy collaboration environment that Google Drive documents provide makes them an interesting back end option for a web form, and Google provides a nice set of APIs for integrating with them.

Here’s how you can easily create a web form that writes to a Google Drive spreadsheet.

  1. Be sure you have a web server that has PHP installed on it.
    If you buy hosting from an online hosting company, you are almost certainly all set. PHP is very common on the internet today and it’s pretty much industry-standard to have it running on your web server. In case you are setting up your own hosting and haven’t used PHP yet, you can download it at You do not have permission to view the full content of this post. Log in or register now..

  2. Create a Google account (if you don’t already have one), log in to Drive, and create a spreadsheet with the columns you want.

    If you go to You do not have permission to view the full content of this post. Log in or register now., you can either sign in with your existing Google account or click the “Sign Up” button in the top right corner to sign up for an account. Once you are signed in, click the “Create” button and then “Spreadsheet” to create a new spreadsheet:
    tumblr_inline_mildb0f8nC1qz4rgp.png

    Next, click on “Untitled spreadsheet” to give your new document a name:
    tumblr_inline_mildj5M8hO1qz4rgp.png

    Finally, type your column headers in row 1 (this will correspond to the fields you want to appear on your web form):
    tumblr_inline_mildo2Jz5L1qz4rgp.png

    The changes will automatically be saved. You can share this document with others on your team you wish to collaborate with, among the other cool things you can do with Google Drive documents.

  3. Download the Zend Gdata Framework and the PHP helper class, and upload them to your web server.

    The Zend Framework is a popular open-source framework for PHP web applications. Dimas at You do not have permission to view the full content of this post. Log in or register now. has written a nifty helper class (which uses Zend) that makes writing to and reading from a Google Drive spreadsheet super easy.

    Download the Zend_Gdata Framework You do not have permission to view the full content of this post. Log in or register now..

    And the Google_Spreadsheet.php helper class at either of these two links:
    A. You do not have permission to view the full content of this post. Log in or register now.
    B. You do not have permission to view the full content of this post. Log in or register now.


    Now, extract the ZendGdata zip file and copy the “Zend” folder from inside the “library” subdirectory. Upload the “Zend” folder and Google_Spreadsheet.php to a folder on your web server:
    tumblr_inline_mo9bvpCtAp1qz4rgp.png

    Important: Make sure you upload the Zend library folder only and make sure it is in the same folder as the Google_Spreadsheet.php file, so the helper class knows where to find it.


  4. Write your PHP web form.
    There are a lot of ways you can do this, but I’m going to use a simple HTML form that submits to itself and uses a PHP script to process the submitted information. Give the file a .php extension so your web server knows it contains PHP code, although this may not be necessary depending on your server’s configuration. Your code will look something like this.

    A basic HTML header with any CSS you want to use:

    HTML:
    <html>
    <head>
       <title>My Web Form</title>
       <link rel="stylesheet" href="css/style.css" type="text/css" />
    </head>

    Next, a PHP script to handle the submitted form and write to the Google Drive spreadsheet:
    PHP:
    <body>
    <?php
       include_once("Google_Spreadsheet.php");
    
       $user = 'MYUSERNAME';
       $pass = 'MYPASSWORD';
    
       $ss = new Google_Spreadsheet($user, $pass);
       $ss->useWorksheet("Sheet1");
       $ss->useSpreadsheet("Email List");
    
       $submitted = $_POST['submitted'];
    
       if ($submitted == 1) {
          $firstname = $_POST['first_name'];
          $lastname = $_POST['last_name'];
          $gender = $_POST['gender'];
          $email = $_POST['email'];
    
          // Do some error checking here if you want
          if (!$email) {
             echo "<h3><font color='red'>*Email Address is required</font></h3>";
          } else {
             // No errors, continue processing registration
    
             $row = array (
                "First Name" => $firstname
                , "Last Name" => $lastname
                , "Gender" => $gender
                , "Email Address" => $email
             );
    
             if ($ss->addRow($row)) {
                // Display success page here
    
                echo "<h1>Thanks for registering!</h1>";
    
                // Send a confirmation email here if you want
             } else {
                // Failed to write to the spreadsheet
                echo "<h1>Sorry there was an error processing your request.</h1>";
             }
          }
       }
    ?>

    Finally, the HTML for the form itself:

    HTML:
    <form id="email_list_form" method="post" action="MYFILENAME.php">
       <h2>Sign up for our email list!</h2>
    
       <label for="first_name">First Name: </label>
       <input id="first_name" name="first_name" type="text" value="<?php echo $firstname; ?>"/><br/>
    
       <label for="last_name">Last Name: </label>
       <input id="last_name" name="last_name" type="text" value="<?php echo $lastname; ?>"/><br/>
    
       <label for="gender">Gender:&nbsp;&nbsp;&nbsp;</label>
       <span>
          <label for="gender_m">Male</label>
          <input id="gender_m" name="gender" type="radio" value="male"/>
          <label for="gender_f">Female</label>
          <input id="gender_f" name="gender" type="radio" value="female"/>
          <br/>
       </span>
    
       <label for="email">Email Address: </label>
       <input id="email" name="email" type="text" value="<?php echo $email; ?>"/><br/>
    
       <input type="hidden" name="submitted" value="1" />
    
       <br/><input type="submit" value="Submit">
    </form>
    
    </body>
    </html>


That’s it. When someone submits the form, it will write the information to the next free row in your Google Drive spreadsheet, where anyone you share it with can view and/or manipulate it as needed. Hopefully this unique approach to a back end will be a useful addition to the options available to you. Let me know what you think.

Source: You do not have permission to view the full content of this post. Log in or register now.
 

Attachments

Status
Not open for further replies.

Similar threads

Back
Top