Lesson 4: Creating Forms – Because Asking for Emails Online is Less Awkward


Lesson4

Lesson 4: Creating Forms – Because Asking for Emails Online is Less Awkward

Welcome to Lesson 4! Now that your webpage has images and links, it’s time to step it up a notch and actually interact with your visitors. How? By creating forms! Whether you want to collect emails, ask for feedback, or even settle the age-old debate of whether pineapple belongs on pizza, HTML forms have you covered (and without any of the awkwardness of asking in person!).

The <form> Tag: The Backbone of Every Form

In HTML, forms are built using the <form> tag. Think of it as a magical box that collects all the data your visitors input and sends it straight to you. Here’s the basic structure:

<form action="your-server-url" method="post">
    <!-- Your form elements go here -->
</form>
  • action: This is where the form data will be sent (usually a server).
  • method: You’ll usually use post (data is sent in the background) or get (data shows up in the URL, like a Google search).

Basic Example:

<form action="/submit" method="post">
    <!-- Form elements -->
    <input type="text" name="name" placeholder="Your Name">
    <input type="email" name="email" placeholder="Your Email">
    <button type="submit">Submit</button>
</form>

Task: Create Your First Form

Try creating a basic form that collects someone’s name and email. Don’t worry about the server part just yet; we’re here to learn the basics (and avoid asking for personal info in person).

Input Fields: Where the Magic Happens

Forms aren’t much without input fields—these are the boxes where people type their info. HTML gives you several types of input fields, but here are a few common ones:

  • <input type="text"> for generic text (like someone’s name).
  • <input type="email"> to ensure the user enters a valid email (or at least tries to).
  • <input type="password"> for hidden fields (like a password or the secret to your chili recipe).

Example with Multiple Input Fields:

<form action="/submit" method="post">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" placeholder="Your Name">
    
    <label for="email">Email:</label>
    <input type="email" id="email" name="email" placeholder="Your Email">
    
    <label for="password">Password:</label>
    <input type="password" id="password" name="password" placeholder="Your Password">
    
    <button type="submit">Register</button>
</form>

Task: Add More Input Fields

Create a form that asks for a name, email, and password. Want to get creative? Ask for their favorite color or their take on pineapple pizza (because it’s a debate that never dies).

Radio Buttons and Checkboxes: Make It a Multiple Choice

Want your visitors to make choices? Use radio buttons or checkboxes:

  • Radio buttons: Users can choose only one option.
  • Checkboxes: Users can pick as many options as they like.

Example with Radio Buttons and Checkboxes:

<form action="/submit" method="post">
    <label>Pick your favorite color:</label><br>
    <input type="radio" id="red" name="color" value="red">
    <label for="red">Red</label><br>
    
    <input type="radio" id="blue" name="color" value="blue">
    <label for="blue">Blue</label><br>
    
    <label>Select your favorite pizzas:</label><br>
    <input type="checkbox" id="margherita" name="pizza" value="margherita">
    <label for="margherita">Margherita</label><br>
    
    <input type="checkbox" id="diavola" name="pizza" value="diavola">
    <label for="diavola">Diavola</label><br>
    
    <button type="submit">Submit</button>
</form>

Task: Add Radio Buttons and Checkboxes

Create a form that lets users choose their favorite color and their favorite pizzas. Because honestly, who doesn’t love a good pizza debate?


Hosting Your Page with Caddy

Now that you’ve added a form to your page, it’s time to host it with Caddy. Here’s how to get it running:

Step 1: Open Your Terminal

Navigate to the directory where your HTML file is saved:

cd ~/my-website

Step 2: Start Caddy

Run this command to host your site locally on port 8080:

caddy file-server --listen :8080

Step 3: View Your Page

Open your browser and go to http://localhost:8080 to check out your new interactive form. You’re now able to gather valuable (or completely random) info from your visitors!


Summary:

In this lesson, you learned how to:

  • Create HTML forms using the <form> tag.
  • Add input fields to collect data from visitors.
  • Use radio buttons and checkboxes to offer choices.
  • Host your updated page using Caddy for testing.

Now your webpage is interactive, and you’re ready to start gathering all sorts of interesting info (like whether people are pro-pineapple or anti-pineapple on pizza!). In the next lesson, we’ll dive into styling your page with CSS. Stay tuned for more web design adventures!


See also