In HTML, forms are used to collect user input and send it to a server for processing (e.g., registration forms, login forms, search boxes).


1. Basic Form Structure

<form action="submit.php" method="post">
  <label for="name">Name:</label>
  <input type="text" id="name" name="username">
  
  <label for="email">Email:</label>
  <input type="email" id="email" name="useremail">
  
  <input type="submit" value="Submit">
</form>

2. Main Form Tags & Attributes

<form>

  • action → URL where form data is sent.
  • method → HTTP method:
    • GET → sends data in the URL (less secure, for search forms).
    • POST → sends data in the body (more secure, for sensitive data).
  • target → where to display the response (_self, _blank, etc.).

3. Common Form Elements

TagPurposeExample
<input type="text">Single-line text box<input type="text" name="username">
<input type="password">Password field (hides characters)<input type="password" name="pass">
<input type="email">Email field (validates format)<input type="email" name="email">
<input type="number">Numeric input<input type="number" min="1" max="10">
<input type="date">Date picker<input type="date">
<textarea>Multi-line text box<textarea name="message"></textarea>
<input type="radio">Radio button (select one)<input type="radio" name="gender" value="male">
<input type="checkbox">Checkbox (select multiple)<input type="checkbox" name="hobby" value="music">
<select> + <option>Drop-down menu<select><option>Option 1</option></select>
<input type="file">File upload<input type="file" name="file">
<input type="submit">Submit button<input type="submit" value="Send">
<input type="reset">Reset form<input type="reset" value="Clear">
<button>Custom button<button>Click Me</button>

4. Example – Registration Form

<form action="register.php" method="post">
  <label for="fname">First Name:</label>
  <input type="text" id="fname" name="firstname" required>

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

  <label for="hobbies">Hobbies:</label>
  <input type="checkbox" name="hobby" value="reading"> Reading
  <input type="checkbox" name="hobby" value="sports"> Sports

  <label for="country">Country:</label>
  <select id="country" name="country">
    <option value="india">India</option>
    <option value="usa">USA</option>
  </select>

  <br><br>
  <input type="submit" value="Register">
</form>

5. Important Points

  • Always give name attributes to form elements — this is how data is sent to the server.
  • Use label with for="id" to make forms accessible.
  • Use required to make a field mandatory.
  • Use CSS for better design (HTML forms are plain by default).

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *