CiTi · Computer Coding · Year One · Unit 3

HTML Forms & Sending Mail: A Contact Form for MySite

Student Walkthrough

The Big Idea

Your MySite contact page is going to work for real. A visitor fills in your form, clicks Send, and a few seconds later the message shows up in your email inbox.

1. BrowserThe visitor fills in your form.
2. SendThe answers travel to a page on your server called send.html.
3. WebDNAsend.html emails the answers to you with [sendmail].
4. DoneYou get the email. The visitor sees a “Thanks!” page.

Day 1 you build and style the form. Day 2 you make it send.

Part A: The Fields

These are the fields your form will have. Fields marked required must be filled in before the form can be sent.

LabelType of boxnameRequired?
NameText boxc_nameYes
EmailEmail boxc_emailYes
PhonePhone boxc_phoneNo
Type of contactDrop-down listc_typeYes
CommentsBig text boxc_commentsYes
(none: visitors never see it)Hidden field, value yesc_sendAlways sent

The c_ at the start of each name stands for “contact.” It makes the form’s names easy to spot in your code.

Why do you think Phone is the only optional field?

Day 1Part B: Build the Form

  1. Open your contact page. In CyberDuck, go to mysite/contact/ and open index.html with Edit with Sublime Text. The top should already look like this from the last lesson:

    [include file=/citi/coding/2027/1/YOURINTS/mysite/includes/vars.inc] [text]titlevar=Contact[/text] [include file=[incvar]header.inc] [include file=[incvar]nav.inc]
  2. Paste the form into your right column. Put it inside <div class="col-right">, replacing whatever is there now.

    <h2>Contact Me</h2> <p>Fields marked <span class="req">*</span> are required.</p> <form class="contact-form" action="[pathvar]contact/send.html" method="post"> <fieldset> <legend>About You</legend> <label for="c_name">Name <span class="req">*</span></label> <input type="text" id="c_name" name="c_name" required> <label for="c_email">Email <span class="req">*</span></label> <input type="email" id="c_email" name="c_email" required> <label for="c_phone">Phone (optional)</label> <input type="tel" id="c_phone" name="c_phone"> </fieldset> <fieldset> <legend>Your Message</legend> <label for="c_type">Type of contact <span class="req">*</span></label> <select id="c_type" name="c_type" required> <option value="">-- Choose one --</option> <option value="Question">Question</option> <option value="Feedback">Feedback</option> <option value="Bug Report">Bug Report</option> <option value="Request">Request</option> <option value="Other">Other</option> </select> <label for="c_comments">Comments <span class="req">*</span></label> <textarea id="c_comments" name="c_comments" rows="6" required></textarea> </fieldset> <input type="hidden" name="c_send" value="yes"> <button type="submit">Send Message</button> </form>
  3. Read what you pasted. Find each of these in your code:

    • <fieldset> draws a box around a group of fields. <legend> is the group’s title. Your form has two groups: About You and Your Message.
    • Every <label> has a for that matches its box’s id. That connects them.
    • name is the name the server sees. A box with no name is never sent.
    • required means the browser won’t send the form while that box is empty.
    • The drop-down’s first choice has value="" (empty). That’s what lets required work on a drop-down.
    • type="hidden" is a box nobody sees. It always sends c_send=yes with the form. That is how send.html will know a message really came from your form. You put it to work in the next lesson.
  4. Save and test in your browser. Open your contact page on your live site. Click a label: the cursor should jump into its box. Click Send Message with everything empty: the browser should stop you and point at the first required box. (Sending does not work yet. That’s Day 2.)

Day 1Part C: Style the Form

  1. Add the form CSS. Open css/style.css and paste this at the very bottom. Read the comments: they tell you what each line does.

    /* ===== Contact form ===== */ .req { color: #d01012; /* red star for required fields */ } .contact-form fieldset { border: 2px solid #333; /* box around each group of fields */ border-radius: 8px; /* rounded corners */ padding: 16px 20px; /* space inside the box */ margin-bottom: 20px; /* space between the two groups */ } .contact-form legend { font-weight: bold; padding: 0 8px; /* space on each side of the group title */ } .contact-form label { display: block; /* each label on its own line */ margin-top: 12px; font-weight: bold; } .contact-form input, .contact-form select, .contact-form textarea { display: block; /* each box on its own line, under its label */ width: 100%; max-width: 500px; /* never wider than 500px */ padding: 8px; /* space inside the box */ font-size: 18px; font-family: inherit; /* use the page's font, not the browser's */ border: 2px solid #999; border-radius: 6px; } .contact-form input:focus, .contact-form select:focus, .contact-form textarea:focus { border-color: green; /* highlight the box you are typing in */ outline: none; /* the green border replaces the default outline */ } .contact-form button { padding: 12px 28px; font-size: 20px; background-color: green; color: white; border: none; border-radius: 6px; cursor: pointer; /* hand cursor, so it looks clickable */ } .contact-form button:hover { background-color: #1b5e20; /* darker green under the mouse */ }
  2. Make it yours. Change the greens to colors that match your site. Then test with the keyboard only: press Tab to move through every box and the button. The box you are in should turn green (or your color).

What does font-family: inherit; fix? (Hint: try deleting it.)

Day 2Part D: Make It Send

Your teacher will give you the From address. Write both addresses here first:

emailvar: my email (where messages arrive)
fromvar: From address from my teacher
  1. Add two settings to vars.inc. Open includes/vars.inc and add these lines at the bottom, using your two addresses. No spaces around the =.

    [!] contact form settings [/!] [text]emailvar=YOUR-EMAIL-ADDRESS[/text] [text]fromvar=ADDRESS-FROM-YOUR-TEACHER[/text]
  2. Make the send page. In CyberDuck, inside mysite/contact/, make a New File named send.html and open it with Sublime. Paste this whole thing in. Change YOURINTS on the first line, and change mail.jpg to one of your own images.

    [include file=/citi/coding/2027/1/YOURINTS/mysite/includes/vars.inc] [text]titlevar=Message Sent[/text] [!] Send the form as an email. This runs every time this page loads. [/!] [sendmail to=[emailvar]&from=[fromvar]&subject=MySite Contact Form] New message from the MySite contact form. Name: [c_name] Email: [c_email] Phone: [c_phone] Type: [c_type] Comments: [c_comments] -- Sent [date format=%B %d, %Y] at [time format=%I:%M %p] from IP [ipaddress] [/sendmail] [include file=[incvar]header.inc] [include file=[incvar]nav.inc] <div class="bodycopy"> <div class="col-left"> <img src="[imgvar]mail.jpg" alt="an envelope"> </div> <div class="col-right"> <h2>Thanks!</h2> <p>Your message was sent. I will get back to you soon.</p> <p><a href="[pathvar]">Back to the home page</a></p> </div> </div> [include file=[incvar]footer.inc]
  3. Read what send.html does:

    • The [sendmail] line has three settings: to (where the email goes), from, and subject. They are joined with &.
    • Everything between [sendmail] and [/sendmail] is the text of the email.
    • Each answer from your form shows up by its name in brackets. name="c_comments" becomes [c_comments].
    • After the email is sent, the page shows a Thanks message using your normal header, nav, and footer.

    This page sends an email every time it loads. It does not check anything yet. In a follow-up lesson you will teach it to check the answers first.

  4. Connect the form to the send page. Your form tag already says action="[pathvar]contact/send.html" and method="post". Check that they are there.

  5. Send yourself a test. Fill in your whole form on your live site and click Send Message. You should see your Thanks page. Check your inbox (and your spam folder). The email should look like this:

    From: (the address in fromvar) To: (your address in emailvar) Subject: MySite Contact Form New message from the MySite contact form. Name: Jordan Diaz Email: jordan@example.com Phone: Type: Feedback Comments: I like your Photos page! -- Sent October 08, 2026 at 10:15 AM from IP 10.0.0.25

    Don’t refresh the Thanks page. The browser will offer to send the form again, and if you say yes, you get a second email.

Day 2Part E: Test the Unhappy Paths

Real developers test what happens when things go wrong, not just when they go right.

Part F: Break It On Purpose

One change at a time. Predict first, test, then undo it before the next one.

1. Remove required from the Name box, then send the form with Name empty

I predict
What happened
Why

2. Change the form to method="get", send it, and look at the address bar

I predict
What happened
Why

3. Delete name="c_comments" from the textarea, then send a complete form

I predict
What happened
Why

4. Change the Name label’s for to for="nme", then click the word Name

I predict
What happened
Why

Form Rules to Remember

Success Criteria Check-In

Check each one when you feel confident you can do it.

Exit Ticket

My form already uses required. How could someone still send me an empty or junk message through send.html? Why does my contact form use POST instead of GET?

My Notes

Use this space for anything: questions, reminders, code you want to keep.