Web Forms
=========
Up until now, we have looked at webpages that simply *display* data. We either
used HTML to create our own page, or we opened up our browser and sent a
``GET`` request to pull up a page we wanted.
Of course, there are plenty of webpages that require us to *supply* data. We
see this anytime we need to fill out a survey, login to one of our accounts, or
sign up online for a new service.
.. index:: ! form
In this chapter we are going to learn more about how web pages handle data
collected from HTML forms. A web **form** is used to accept input from a user
and send that data to a server.
Create a Form
-------------
To declare a form in HTML, we use the ``
#. Once the new page renders, notice that a box appears. Clicking inside
this box allows us to type something!
Local HTML Forms
^^^^^^^^^^^^^^^^
We used an embedded Trinket editor above just to provide a quick start. Let's
move this into Visual Studio Code, since we will do most of our work locally
from now on.
#. Launch VS Code. Open your ``local_practice`` folder and create a new
directory called ``forms_chapter``.
#. Navigate into ``forms_chapter`` and initialize it as a new repository with
``git init``.
#. Create an ``index.html`` and ``style.css`` file inside the folder.
#. Paste your code from the Trinket editor above into the ``index.html`` file.
#. :ref:`Save and commit ` your work.
#. Open ``index.html`` in a new tab of your browser to confirm that your setup
works.
#. Use ``git branch`` to check the name of your default branch. If necessary,
use ``git branch -m old-branch-name main`` to change its name to ``main``.
.. _input-tag:
Input Element
-------------
.. index:: input
The ``input`` element adds an *interactive* field, which allows the user to
enter data. ``input`` elements have two very important attributes: *name* and
*type*.
#. The ``name`` attribute identifies the input's value when the form is
submitted.
#. The ``type`` attribute defines the value expected from the input box. These
value types include text, numbers, dates, email addresses, etc.
The syntax for an input tag is:
.. sourcecode:: html
For the example, in the HTML code above, we could use this for the ``input``
element:
.. sourcecode:: html
.. index:: ! self-closing
Notice that ``input`` tags are **self-closing**. This means that the element
only requires a *single* tag, which ends with ``/>``.
.. admonition:: Warning
Values are NOT submitted for an ```` unless it includes a ``name``
attribute.
Labels
------
.. index:: ! label
single: input; field
Right now, our HTML form contains a single input box (also called a **field**).
However, we don't actually tell the user what they need to type into that box.
Not good!
To make the user's experience better, we need to provide them with a clear idea
of what goes into each field. This is especially true if our form contains more
than one input!
``