Showing posts with label classnotes. Show all posts
Showing posts with label classnotes. Show all posts

Monday, 9 December 2024

Lecture Notes Of Class 6: Forms - Part 1 (Basic Elements)

 


Lecture Notes Of  Class 6: Forms - Part 1 (Basic Elements)

Objective:

By the end of this class, students will understand the basic structure and elements of HTML forms. They will be able to create simple forms using input fields, labels, and the required attributes to ensure user interaction and validation.


1. Introduction to HTML Forms

Forms are essential in web development as they allow users to input data that can be sent to a server for processing. A form might ask for user details such as name, email, password, and other data that can be submitted for various purposes like login, registration, or feedback.

In HTML, a form is created using the <form> tag. Inside the form, we use various input elements to collect data.


2. Basic Structure of a Form

A basic HTML form looks like this:

html

Copy code

<form action="submit_form.php" method="post">

  <!-- Form elements will go here -->

</form>

  • <form>: This tag creates the form.
    • action: Specifies the URL where the form data will be sent for processing.
    • method: Defines the HTTP method (either "GET" or "POST") used to send form data.

3. Basic Form Elements

Let's now go over the most common elements used in forms.

3.1. Input Fields

The <input> tag is used to create interactive controls within a form. The most common types are:

  • Text Fields: For users to enter text (like names or email addresses).

html

Copy code

<input type="text" name="username">

  • Password Fields: These hide the characters entered for security purposes (used for passwords).

html

Copy code

<input type="password" name="password">

  • Email Fields: Specifically for email addresses. It also validates the format of the entered email.

html

Copy code

<input type="email" name="email">

  • Submit Button: Used to submit the form.

html

Copy code

<input type="submit" value="Submit">

Each <input> element should have a name attribute. This is used to identify the data when it is submitted.


3.2. Labels

Labels are used to describe the purpose of an input field. They are essential for accessibility and help screen readers identify the form fields. The <label> tag is used to create labels.

html

Copy code

<label for="email">Email:</label>

<input type="email" id="email" name="email">

  • for attribute: The for attribute connects the label to an input field. It must match the id of the corresponding input element. This helps users click on the label to focus on the input field.

4. Creating a Login Form

Let's create a simple login form using the elements we've discussed.

html

Copy code

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Login Form</title>

</head>

<body>

  <h2>Login</h2>

 

  <!-- Form starts here -->

  <form action="login.php" method="post">

   

    <!-- Email input field -->

    <label for="email">Email:</label>

    <input type="email" id="email" name="email" placeholder="Enter your email" required>

   

    <br><br>

   

    <!-- Password input field -->

    <label for="password">Password:</label>

    <input type="password" id="password" name="password" placeholder="Enter your password" required>

   

    <br><br>

   

    <!-- Submit button -->

    <input type="submit" value="Login">

  </form>

 

</body>

</html>

Explanation:

  • Action and Method: The form will be submitted to the login.php file, and the POST method is used to send the data.
  • Email Field: The type="email" ensures that the input is validated to be an email address. The placeholder gives users a hint about what to enter. The required attribute ensures that the field is not empty before the form can be submitted.
  • Password Field: Similar to the email field, but the type="password" hides the input text. The placeholder provides guidance on what to enter, and required ensures it's not empty.
  • Submit Button: The submit button will send the form data to the server.

5. Using Placeholders and Required Attributes

5.1. Placeholders

The placeholder attribute is used to provide a hint inside the input field that shows a short description of the expected value. It disappears when the user starts typing.

Example:

html

Copy code

<input type="email" name="email" placeholder="Enter your email">

5.2. Required Attribute

The required attribute is used to ensure that the user cannot submit the form without filling out the field. If the field is left empty, the browser will prevent form submission and will show a message asking the user to fill in the required fields.

Example:

html

Copy code

<input type="password" name="password" required>


6. Lab Tasks

Task 1: Create a Login Form

1.   Create a basic login form with input fields for the user's email and password.

2.   Add labels for each input field.

3.   Use placeholders to guide the user about what to input.

4.   Add the required attribute to both the email and password fields to ensure they are not left empty.

5.   Include a submit button to submit the form.

Expected Outcome:

Your login form should look like this:

  • Email input field with a placeholder ("Enter your email").
  • Password input field with a placeholder ("Enter your password").
  • Both fields should be required.
  • A submit button that sends the form data to a processing page.

Task 2: Test the Form

  • Open your form in a browser and ensure that the placeholder text appears inside the input fields.
  • Test the form by leaving the fields empty and trying to submit. You should see a browser warning if the required attribute is used correctly.

7. Summary

In this lesson, we learned how to create a basic HTML form with the essential elements:

  • Input fields for collecting data.
  • Labels for describing each input field.
  • Placeholders to give hints about what the user should enter.
  • The required attribute to ensure that users fill out the necessary fields.

These basic form elements form the foundation of any user interaction on a website, from simple login forms to more complex data entry forms.



Lecture Notes Of Class 5 - Buttons and Alerts


Lecture Notes Of Class 5 - Buttons and Alerts


Objective

By the end of this class, students will understand how to use different button styles and sizes in HTML and CSS (or Bootstrap) and display alerts for success, error, and warnings.


1. Introduction to Buttons

Buttons are an essential part of any web application, allowing users to interact with the system by performing actions like submitting forms, navigating pages, or triggering events.

Types of Buttons

Buttons can be categorized based on their styles, purposes, or the frameworks used to style them. If using Bootstrap, buttons are styled using predefined classes such as:

  • Primary Button: Indicates a primary action.
  • Secondary Button: For less important actions.
  • Success Button: Indicates a successful or positive action.
  • Danger Button: Indicates a potentially negative action.
  • Warning Button: For cautionary actions.
  • Info Button: Displays informational messages.
  • Light Button: Minimal styling, usually light background.
  • Dark Button: Dark background, opposite to light.
  • Link Button: Styled as a hyperlink.

2. Button Styles and Sizes

Basic Syntax for Buttons

Buttons are created using the <button> tag or <a> tags styled as buttons. Examples:

html

Copy code

<!-- Default Button -->

<button>Click Me</button>

Using Classes for Styling (Bootstrap)

Bootstrap provides predefined classes to style buttons.

html

Copy code

<!-- Example Buttons -->

<button class="btn btn-primary">Primary</button>

<button class="btn btn-secondary">Secondary</button>

<button class="btn btn-success">Success</button>

<button class="btn btn-danger">Danger</button>

<button class="btn btn-warning">Warning</button>

<button class="btn btn-info">Info</button>

<button class="btn btn-light">Light</button>

<button class="btn btn-dark">Dark</button>

<button class="btn btn-link">Link</button>

Button Sizes

Buttons can be adjusted in size using additional classes.

  • Large Button: btn-lg
  • Small Button: btn-sm
  • Default Button: No size class needed.

Example:

html

Copy code

<button class="btn btn-primary btn-lg">Large Button</button>

<button class="btn btn-primary btn-sm">Small Button</button>

<button class="btn btn-primary">Default Button</button>

Button States

You can disable a button to prevent user interaction using the disabled attribute or class.

html

Copy code

<button class="btn btn-primary" disabled>Disabled Button</button>


3. Alerts

Alerts are used to display important messages to users, such as notifications, warnings, or errors. In Bootstrap, alerts are styled using predefined classes.

Basic Syntax for Alerts

Alerts are created using the <div> tag with the alert class and a contextual class for the alert type.

html

Copy code

<div class="alert alert-success" role="alert">

  This is a success alert—check it out!

</div>

<div class="alert alert-danger" role="alert">

  This is a danger alert—be careful!

</div>

<div class="alert alert-warning" role="alert">

  This is a warning alert—pay attention!

</div>

Types of Alerts

  • Success Alert: alert-success (e.g., "Operation successful!")
  • Danger Alert: alert-danger (e.g., "An error occurred!")
  • Warning Alert: alert-warning (e.g., "Proceed with caution!")
  • Info Alert: alert-info (e.g., "Information update.")
  • Light and Dark Alerts: alert-light, alert-dark

Dismissing Alerts

Alerts can be made dismissible (closable) using the alert-dismissible class and a close button.

html

Copy code

<div class="alert alert-warning alert-dismissible fade show" role="alert">

  Warning! This is a dismissible alert.

  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>

</div>


Lab Tasks

Task 1: Add Buttons with Various Styles

1.   Create a webpage with the title "Button Styles and Alerts."

2.   Add buttons with the following classes:

o    btn-primary

o    btn-danger

o    btn-success

o    btn-warning

3.   Use large, small, and default sizes for at least one button.

Task 2: Display Alerts for Success, Error, and Warnings

1.   Add a success alert with the message: "Your operation was successful!"

2.   Add a danger alert with the message: "An error occurred while processing your request!"

3.   Add a warning alert with the message: "Please proceed with caution!"

4.   Make one of the alerts dismissible.


Code Example: Combined Buttons and Alerts

html

Copy code

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Buttons and Alerts</title>

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

  <div class="container mt-5">

    <h2>Button Styles</h2>

    <button class="btn btn-primary">Primary</button>

    <button class="btn btn-danger btn-lg">Large Danger</button>

    <button class="btn btn-success btn-sm">Small Success</button>

 

    <h2 class="mt-5">Alerts</h2>

    <div class="alert alert-success" role="alert">

      This is a success alert—check it out!

    </div>

    <div class="alert alert-danger alert-dismissible fade show" role="alert">

      This is a dismissible danger alert!

      <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>

    </div>

    <div class="alert alert-warning" role="alert">

      Warning! Pay attention!

    </div>

  </div>

 

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>

</body>

</html>


Learning Outcomes

  • Students will understand how to style buttons using predefined classes.
  • Students will be able to create alerts with various contextual styles.
  • Students will understand how to make alerts dismissible for better user interaction.


Search This Blog

Powered by Blogger.

Labels