Friday, August 22, 2025

GitHub Pages: Google Form for Contact Form

How to Add a Google Form to a GitHub Pages Site

Using a Google Form on your GitHub Pages site is a simple and secure way to gather user information without needing a backend server or exposing your personal email address. This guide will walk you through the process, from creating the form to embedding it in your HTML.

Why Use a Google Form?

GitHub Pages is a static hosting service, which means it doesn't support server-side scripts like PHP or Node.js. This can make it tricky to handle form submissions. Google Forms provides a free and secure solution by handling the data collection for you and storing it in a Google Sheet.

Step 1: Create Your Google Form

  1. Go to Google Forms: Open a new form at forms.google.com.
  2. Add Your Questions: Create the fields you need, such as "Name," "Email," and "Message."
  3. Get the Embed Code: Click the purple "Send" button in the top-right corner, then select the `< >` (Embed HTML) tab.
  4. Copy the Code: Copy the `<iframe>` code that Google provides. This code contains a unique URL for your form.

Step 2: Prepare Your HTML File

Open your index.html file on your local machine or directly in your GitHub repository. The goal is to insert the Google Form `<iframe>` into a section where you want it to appear.

Here is a simple, responsive HTML template you can use as a starting point:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Landing Page</title>
</head>
<body>
    
    <div>
        
        <!-- Main content here -->
        <div>
            <h1>
                Your Awesome Service
            </h1>
            <p>
                Discover a new way to streamline your workflow and boost productivity.
            </p>
        </div>

        <!-- This is where your form will go -->
        <div>
            <div>
                <h2>
                    Get in Touch
                </h2>
                
                <!-- Paste your Google Form <iframe> code here -->
                <iframe 
                    src="YOUR_GOOGLE_FORM_EMBED_URL_HERE" 
                    width="100%" 
                    height="500" 
                    frameborder="0" 
                    marginheight="0" 
                    marginwidth="0"
                    loading="lazy">Loading…</iframe>

                <div>
                    Your information is submitted securely to a Google Form.
                </div>
            </div>
        </div>
    </div>

</body>
</html>

Step 3: Embed the Form

Now, take the `<iframe>` code you copied from Google Forms and replace the placeholder `src` value in the template above.

For example, if your code looks like this:

<iframe src="https://docs.google.com/forms/d/e/1FAIpQLSe-0h0w7pXbYyqC-D5WJ8T3K4fMvFzYn8JdG-d_x-d_x-d/viewform?embedded=true" width="640" height="850" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>

You would just need to copy the URL inside the `src` attribute and paste it into the `<iframe>` in your HTML file.

<iframe 
    src="https://docs.google.com/forms/d/e/1FAIpQLSe-0h0w7pXbYyqC-D5WJ8T3K4fMvFzYn8JdG-d_x-d_x-d/viewform?embedded=true" 
    width="100%" 
    height="500" 
    frameborder="0" 
    marginheight="0" 
    marginwidth="0"
    loading="lazy">Loading…</iframe>

Note: The width and height attributes have been adjusted to `100%` and `500` respectively, to make the form more responsive within the page layout.

Step 4: Save and Push to GitHub

Save the changes to your HTML file, commit them, and push them to your GitHub repository. GitHub Pages will automatically rebuild your site, and your new form will be live!

That's it! You've successfully added a secure contact form to your static GitHub Pages site.

Final Thoughts

This approach provides a reliable, secure, and free way to add contact forms to any static website, not just those hosted on GitHub Pages. It's a great example of how you can leverage third-party tools to add dynamic functionality to a static site without needing to write any backend code. If you want to take it a step further, you can explore services like Formspree or Netlify Forms, which offer more customization and direct integration, but for most projects, this simple Google Forms solution is more than enough to get the job done.

No comments:

Post a Comment

Building BlueMountainDJ.com: A Modern Wedding DJ Website with Tailwind CSS and JavaScript

Building BlueMountainDJ.com: A Modern Wedding DJ Website with Tailwind CSS and JavaScript As a web developer, I recently had the opportunit...