7. Forms

So far visitors have a passive position since information travels one way from the hosting to their computers; they don't have tools to reverse the roles: they can read but not respond. The solution is to use forms, some interactive fields with the ability to collect information, provided by the user, to send it to the server. What happens next with that data doesn't concern us, it's not the Web Designer's job. Such is the separation of responsibility that there is even a specialization in charge of processing and storing the information: Back-End.

However there are other extremely important tasks we must pay special attention to:

  • Give the appropriate structure.
  • Set up an orderly and self-explanatory flow.
  • Entertain the visitor.
  • Work on accessibility.

Every form has 4 states.

  1. Presentation: Usually left empty, with some visual aid in the form of text or iconography that explains each section.
  2. Validation: If a field doesn't meet the desired format we must inform and guide so that the user can adjust it. A recurring resource is to show a red text with a brief explanation and an example.
  3. Sent without problems: When clicking the submit button no validation was triggered and the web server confirms that it has received the information correctly. The user is informed and, if possible, taken to another section of the site.
  4. Sent with problems: Oops! When the information was sent, the unimaginable happened: the web server tells us that it failed for some reason while processing the information. We must warn the user and bow our head in apology. The recommended thing would be to give an alternative option: a phone? an email?

Container

Every form must be wrapped in the following tag.

Tag <form>
Description Container of interactive elements that give the visitor the option to send information.
Attributes action: Path where the information will be sent.
enctype: Format of the content. Its options are application/x-www-form-urlencoded, multipart/form-data and text/plain
method: HTTP method the browser will use to send the information. Its options are: get and post.
autocomplete: Tells the browser whether or not it can use its autocompleter. Its options are on and off
novalidate: Indicates whether or not the information should be validated before being sent.
Type Block
<form>
    <!-- Interactive fields -->
</form>

Inside we'll use various tags, each one specialized in a domain.

Insert text

Tag <label>
Description Works together with <input>. Describes its content.
Attributes for: Links with the id attribute of an input.
Type Inline
Tag <input>
Description Form field for inserting content such as text.
Attributes type: Defines the type.
name: Name by which it will be represented when it reaches the server (must always be present).
value: Content or text.
required: Forces it to be filled in.
placeholder: Informative text.
disabled: Disables the element, although it's visible.
Type Inline

The most popular values of type are:

  • button
  • checkbox
  • color
  • date
  • email
  • file
  • number
  • password
  • radio
  • range
  • reset
  • search
  • submit
  • text
<label>
    What is your favorite radio station?
    <input type="text" name="cadenaRadio">
</label>

As an alternative, if you couldn't nest the tags, you can link them with the for and id attributes.

<label for="miRadio">What is your favorite radio station?</label>

<input id="miRadio" type="text" name="cadenaRadio">
Tag <textarea>
Description Text field for several lines
Attributes Global
Type Inline
<label>
    Notes:
    <textarea name="anotaciones"></textarea>
</label>

Select

Tag <select>
Description Container of options.
Attributes Global
Type Inline
Tag <option>
Description Option of the selector.
Attributes value: Value that will be sent.
disabled: Prevents it from being selectable.
selected: Selects the option.
Type Inline
<select name="mascota">
    <option value="0">Dog</option>
    <option value="1">Cat</option>
    <option value="2">Hamster</option>
    <option value="3">Rabbit</option>
    <option value="4">Turtle</option>
</select>

A trick to have a title that afterwards can't be selected again is to use the disabled and selected attributes on the same <option>.

<select name="mascota">
    <option value="" aria-label="pet" disabled selected>What pet do you have?</option>
    <option value="0">Dog</option>
    <option value="1">Cat</option>
    <option value="2">Hamster</option>
    <option value="3">Rabbit</option>
    <option value="4">Turtle</option>
</select>

Buttons

Tag <button>
Description Triggers events on the form.
Attributes type: "submit" to send the form, "reset" to reset the fields or "button" to trigger functionalities in JavaScript.
Type Inline
<button type="submit">
    Send
</button>

It's easier to style <button> than <input type="button"> since you can include tags in the nesting such as <img> and use pseudo-elements (::after, ::before...).

Group fields

Tag <fieldset>
Description Section of fields to group
Attributes Global
Type Inline
Tag <legend>
Description Group title
Attributes Global
Type Inline
<fieldset>
  <!-- Fieldset title -->
  <legend>Ticket reservation</legend>

  <label>
      Full name
      <input type="text" name="nombre">
  </label>

  <label>
      Number of seats
      <input type="number" name="numero">
  </label>

  <button type="submit">Pay</button>
</fieldset>
Ticket reservation

Activity 1

Create a contact form with the fields you consider appropriate. The information that will be sent is the following.

  • Name
  • Email
  • Message
Activity 2

Create a form, with the fields you consider appropriate, for a store that can sell its speakers. The information that will be sent is the following.

  • Model: R4, R5 or SR1.
  • Maximum power: between 50 and 100.
  • Bluetooth?
  • Color.
Activity 3

Create a form, with the fields you consider appropriate, so that a user can register on your social network.

Activity 4

Create a form, with the fields you consider appropriate, to search for cameras.

This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.

Desafíos de programación atemporales y multiparadigmáticos

Desafíos de programación atemporales y multiparadigmáticos

Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.

Buy the book

Will you buy me a coffee?

This is how I keep writing without ads or paywalls.

Comments

There are no comments yet.