HTML Forms

Forms are one of the most powerful tools in HTML, allowing users to input data and interact with websites. From simple login screens to complex surveys, forms are essential. Let's dive into the basics of creating forms in HTML.

1. The <form> Element

The <form> tag is the container for all form elements. It defines where and how the data will be sent. Here's a basic structure:

HTMLcopy
<form action="submit.php" method="post">
  <!-- Form elements go here -->
</form>
  • action: Specifies where the form data will be sent (e.g., a server script).
  • method: Defines how data is sent (GET for appending to the URL, POST for secure submission).

2. Common Form Elements

Here are some commonly used input elements:

Text Input

HTMLcopy
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your name">
  • type="text": Creates a single-line text box.
  • placeholder: Displays a hint inside the input box.
ul.ul

Password Input

HTMLcopy
<label for="password">Password:</label>
<input type="password" id="password" name="password">

Hides the text as dots or asterisks.

Radio Buttons

HTMLcopy
<label>
  <input type="radio" name="gender" value="male"> Male
</label>
<label>
  <input type="radio" name="gender" value="female"> Female
</label>

Checkboxes

HTMLcopy
<label>
  <input type="checkbox" name="subscribe" value="newsletter"> Subscribe to newsletter
</label>

Dropdown (Select)

HTMLcopy
<label for="country">Country:</label>
<select id="country" name="country">
  <option value="usa">USA</option>
  <option value="canada">Canada</option>
  <option value="uk">UK</option>
</select>

Submit Button

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

Example

Here's a simple form in action:

HTMLcopy
<form action="/submit" method="post">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" placeholder="Enter your username">

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" placeholder="Enter your email">

  <label>
    <input type="checkbox" name="terms"> I agree to the terms and conditions
  </label>

  <input type="submit" value="Sign Up">
</form>




Forms are very essential for collecting and processing user data.

Page 29 of 33

Page 29 of 33

logoFALYTOM

Learn technologies like HTML, CSS, JavaScript, React, NodeJS and so much more. Practice your skills with our free games, examples, practice questions & quizzes

    © 2024 Tomilola Group
    • facebook logo
    • twitter logo
    • instagram logo
    • linkedin logo
    • github logo
    • youtube logo