Course Content
Introduction to HTML (Structure of the Web)
0/1
Introduction to CSS (Styling the Web)
Getting Started with CSS
0/2
Introduction to JavaScript (Interactivity on the Web)
Project Presentation and Review
0/1
Protected: Web Development Bootcamp: Master HTML, CSS, and JavaScript in 3 Weeks
About Lesson

1. Basics of Forms in HTML: <form>, <input>, <textarea>

Forms are essential components of web pages that allow users to input data. In HTML, the most commonly used elements for creating forms are <form>, <input>, and <textarea>. Let’s look at each of these elements and how they work together to create functional forms.


1. The <form> Element

The <form> element is the container for all form elements. It defines a section of the page where users can enter data that will be sent to the server for processing. The form typically includes input fields, buttons, and other elements.

Attributes of the <form> element:

  • action: Specifies the URL where the form data will be submitted. This is usually a server-side script (e.g., PHP, Python) that processes the data.
  • method: Specifies the HTTP method used to submit the form data. Common methods are GET and POST.
    • GET: Appends data to the URL (suitable for non-sensitive data).
    • POST: Sends data in the request body (suitable for sensitive or large data).

Example:

<form action="submit_form.php" method="POST">
  <!-- Form elements go here -->
</form>

2. The <input> Element

The <input> element is one of the most commonly used form elements. It can take various types, such as text, password, checkbox, radio, and more, depending on the type attribute. Each input type is used to collect different kinds of user data.

Common Types of <input> Elements:

  • type="text": A single-line text input.
  • type="password": A password input field that hides the entered text.
  • type="email": An email input that validates email format.
  • type="checkbox": A checkbox for selecting options.
  • type="radio": A radio button for selecting one option from a group.
  • type="submit": A button that submits the form.
  • type="reset": A button that resets all form fields to their initial values.

Example:

<form action="submit_form.php" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required><br><br>

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required><br><br>

  <input type="submit" value="Submit">
</form>
  • type="text": Allows users to enter a short text string (e.g., name).
  • type="email": Ensures that the entered data matches an email format (e.g., user@example.com).
  • required: Ensures the field must be filled out before submission.

3. The <textarea> Element

The <textarea> element is used to create multi-line text input fields. It allows users to input longer pieces of text, such as comments or messages. Unlike the <input> element, <textarea> supports multiple lines of text and provides a scrollable area for the user to enter more information.

Attributes of the <textarea> Element:

  • rows: Specifies the number of visible rows (lines) in the textarea.
  • cols: Specifies the number of visible columns (width) in the textarea.
  • placeholder: Provides a short hint within the textarea to indicate the type of information the user should enter.

Example:

<form action="submit_form.php" method="POST">
  <label for="message">Message:</label><br>
  <textarea id="message" name="message" rows="4" cols="50" placeholder="Enter your message here"></textarea><br><br>

  <input type="submit" value="Submit">
</form>
  • rows="4": Makes the textarea display four visible rows of text.
  • cols="50": Makes the textarea 50 characters wide.
  • placeholder: Provides a hint inside the textarea before the user starts typing.

Summary of Form Basics

  • <form>: The container element for all form elements. Defines how the form will send data and where.
  • <input>: A versatile element used for various types of user input, such as text, passwords, emails, checkboxes, etc.
  • <textarea>: Used for multi-line text input, such as for comments or messages.

These basic form elements work together to create interactive forms that collect user input.

 

 

2. Buttons and Form Submission in HTML

Buttons are a critical part of web forms, allowing users to submit data, reset forms, and trigger various actions. In HTML, buttons can be used in conjunction with forms to submit or reset the form data. Let’s go over the key elements for buttons and form submission:


1. The <button> Element

The <button> element is a versatile HTML element used to create clickable buttons. It can be used for submitting a form, resetting it, or triggering other JavaScript functions. The <button> element can be styled and customized more easily than the <input> element.

Attributes of the <button> Element:

  • type: Defines the button’s purpose.

    • type="submit": Submits the form data.
    • type="reset": Resets all form fields to their initial values.
    • type="button": A generic button that doesn’t submit or reset the form. It is commonly used with JavaScript for custom actions.
  • name: The name of the button, which can be used to reference it in JavaScript.

  • value: The value sent to the server when the button is clicked.

Example:

<form action="submit_form.php" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required><br><br>

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required><br><br>

  <!-- Submit Button -->
  <button type="submit">Submit</button>

  <!-- Reset Button -->
  <button type="reset">Reset</button>

  <!-- Custom Button (doesn't submit or reset the form) -->
  <button type="button" onclick="alert('Button clicked!')">Click Me</button>
</form>
  • type="submit": Submits the form.
  • type="reset": Resets the form fields.
  • type="button": Executes JavaScript actions without submitting or resetting the form.

2. The <input> Element with type="submit"

The <input> element can also be used to create a submit button, but it offers less flexibility in terms of styling compared to the <button> element.

Attributes of <input> with type="submit":

  • type="submit": This type makes the button submit the form data.
  • value: The text that will appear on the submit button.

Example:

<form action="submit_form.php" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required><br><br>

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required><br><br>

  <!-- Submit Button -->
  <input type="submit" value="Submit Form">
</form>
  • type="submit": The button submits the form.
  • value="Submit Form": The text shown on the submit button.

3. Form Submission and Action

When a user clicks the submit button (either <button type="submit"> or <input type="submit">), the form data is sent to the server specified in the form’s action attribute. The method attribute determines how the data is sent (via GET or POST).

  • GET: Appends the form data to the URL. Suitable for non-sensitive data.
  • POST: Sends the form data as part of the request body. Suitable for sensitive data (e.g., passwords).

Example:

<form action="submit_form.php" method="POST">
  <!-- Form Fields -->

  <button type="submit">Submit Form</button>
</form>

In this example, when the user clicks the “Submit Form” button:

  • The form data will be sent to the submit_form.php page on the server.
  • The method="POST" indicates that the data will be sent securely.

4. Form Reset Button

A reset button is used to clear all the fields in a form, resetting them to their default values. This can be useful when a user wants to start over without manually clearing each field.

Example:

<form action="submit_form.php" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required><br><br>

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required><br><br>

  <!-- Reset Button -->
  <button type="reset">Reset</button>
</form>

When clicked, the type="reset" button clears all form fields.


5. Custom JavaScript Button

Sometimes, you may want to use a button to trigger a custom JavaScript function instead of submitting or resetting the form. This can be done with the type="button" attribute.

Example:

<form action="submit_form.php" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required><br><br>

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required><br><br>

  <!-- Custom JavaScript Button -->
  <button type="button" onclick="validateForm()">Submit</button>
</form>

<script>
  function validateForm() {
    alert("Form submitted!");
    // Custom form validation or actions can go here.
  }
</script>

This button will trigger the validateForm() JavaScript function instead of submitting the form.


Summary

  • <button type="submit">: Submits the form.
  • <button type="reset">: Resets the form fields.
  • <input type="submit">: Another way to create a submit button.
  • <input type="reset">: Resets all form fields to their initial values.
  • <button type="button">: A generic button used for custom actions, often with JavaScript.

Buttons are essential for form submission, resetting, and custom actions, and they can be styled and customized for a better user experience.