Submitting Forms ================ .. index:: single: form; submission Forms collect data from the user. Our first basic web form included two simple text inputs, one for a ``Username`` and one for a ``Team Name``. We can type whatever we want into the two fields, but that's it. The form doesn't actually *send* the information anywhere. As we learned in the :ref:`HTTP chapter `, actions on the web involve a series of *requests* and *responses*. When we click on a link or enter a URL in the browser's address bar, we request a specific page stored on a server somewhere. Data moves from the server to our browser (the client). A **form submission**, on the other hand, is an HTTP request that contains values collected from a form. In this case, data moves from the client to the server. To collect the data entered by a user and send it on its way, we need to *trigger* the form submission. Submit a Form ------------- Form submission is triggered by clicking a button. This often has a label like *Submit*, *OK*, or *Login*. Regardless of the label, we call this a ``submit`` button. We add it to our forms one of two ways: #. Using a separate ``input`` element with the ``type="submit"`` attribute. #. Using a ``button`` element. Each of these methods is shown in the box below. .. admonition:: Example Here is our basic form again, but this time we added two buttons inside the element. .. sourcecode:: html :lineno-start: 13


.. figure:: figures/two-form-buttons.png :alt: Form with two text inputs and two buttons ("Submit Query" and "Login") Adding two buttons to our basic form. Note that the ```` syntax automatically adds the words ``Submit Query`` inside the button. To change the text, we add the ``value="button-text"`` attribute inside the tag and replace ``button-text`` with the label we want. The `` #. Save your work, then open ``index.html`` in your browser. #. Note that anything you type into the input appears as plain text. This is NEVER a good idea when dealing with passwords. To fix this, change the ``type="text"`` attribute to ``type="password"``. #. Save the change, then refresh the page in your browser. Now what do you see as you type in the input box? .. raw:: html
#. By changing the ``type`` in the ``input`` tag, we hide what the user enters in the field! *HOWEVER*... Enter a simple string of characters into your form and click *Login*. Now check the updated URL. .. figure:: figures/pw-in-url.png :alt: A URL in the browser address bar, showing a submitted password. :width: 80% Yikes! There's a password in the address bar. Even though your password was hidden inside the form, it appears as plain text in the URL after submitting. We need to fix this. Even if nobody is looking over your shoulder to see the URL, it is possible to intercept an HTTP request on its way to the server. If that happens, someone could capture your password. The reason the password appears in the address bar is due to another default setting. When we submit a form, it automatically creates a ``GET`` request, which adds the query string to the URL. In the next section, we will learn how to submit a form with a ``POST`` request. Unlike ``GET``, ``POST`` hides form data. Check Your Understanding ------------------------ .. admonition:: Question What must be added to a ``form`` element in order to submit the collected data? .. raw:: html
  1. A name attribute.
  2. A button.
  3. An action attribute.
  4. A POST request.

.. Answer = b .. admonition:: Question By default, are HTTP forms submitted as ``GET`` or ``POST`` requests? .. raw:: html
  1. GET
  2. POST

.. Answer = b