HTML Forms

HTML Forms are the foundation of user interaction on the web. They allow visitors to input information, such as names, emails, passwords, or feedback, and send it to a server for processing.

Forms make websites dynamic and interactive. They are essential for:

  • User registration and login
  • Search bars
  • Feedback and contact forms
  • Online surveys
  • Payment gateways
  • E-commerce checkouts

Without forms, a website would be just a static page with no user engagement.

Basic Syntax of an HTML Form

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

Explanation:

AttributeDescription
actionSpecifies where to send the form data (server or file).
methodDefines how the data will be sent – GET or POST.
nameGives the form a name for identification.
targetSpecifies where to display the response after submission.

Form Submission Methods

MethodDescriptionData Visibility
GETAppends form data to the URL. Used for searching or filtering.Visible in address bar
POSTSends form data invisibly inside the request body. Best for passwords, login, or sensitive data.Hidden from users

Example of GET:

https://example.com/search?query=HTML

Example of POST:

Data sent internally – not shown in URL.

Common HTML Form Elements

HTML provides various input elements to capture user data.

1. <input> Element

The most commonly used tag in forms.

<input type=”text” name=”username” placeholder=”Enter your name”>

Types of input:

TypeDescription
textFor single-line input
passwordHides typed characters
emailAccepts only valid email formats
numberAllows numeric input
checkboxMultiple choices allowed
radioSingle choice among multiple options
fileUploads files
submitSends the form data
resetClears the input fields

2. <label> Element

Labels improve accessibility and help screen readers understand input fields.

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

The for attribute connects the label to the corresponding input ID.

3. <textarea> Element

Used for multi-line text input (like comments or messages).

<textarea name="message" rows="5" cols="30" placeholder="Enter your message"></textarea>

4. <select> Dropdown List

Used when users need to choose from multiple options.

<select name="country">
  <option value="india">India</option>
  <option value="usa">USA</option>
  <option value="uk">UK</option>
</select>

5. <button> Element

Creates clickable buttons inside a form.

<button type="submit">Submit</button>
<button type="reset">Reset</button>

Complete HTML Form Example

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Learn how to create HTML forms with examples of input fields, labels, buttons, and form submission methods.">
    <meta name="keywords" content="HTML forms, HTML form elements, input tags, form attributes, GET POST methods, HTML tutorial">
    <meta name="author" content="Dharmesh Kundariya">
    <title>HTML Form Example</title>
</head>

<body>
    <h1>HTML Form Example</h1>
    <form action="submit_form.php" method="post"> <label for="name">Full Name:</label><br> <input type="text" id="name" name="fullname" placeholder="Enter your name" required><br><br> <label for="email">Email:</label><br> <input type="email" id="email" name="email" placeholder="Enter your email" required><br><br> <label for="country">Country:</label><br> <select id="country" name="country">
            <option value="india">India</option>
            <option value="usa">USA</option>
            <option value="uk">UK</option>
        </select><br><br> <label>Gender:</label><br> <input type="radio" name="gender" value="male"> Male <input type="radio" name="gender" value="female"> Female<br><br> <label for="message">Message:</label><br> <textarea id="message" name="message" rows="4" cols="30" placeholder="Write your message..."></textarea><br><br> <input type="submit" value="Submit Form"> <input type="reset" value="Clear Form"> </form>
</body>

</html>

Example of GET vs POST in Action

GET Example

<form action="search.php" method="get">
  <input type="text" name="query" placeholder="Search...">
  <button type="submit">Search</button>
</form>

POST Example

<form action="login.php" method="post">
  <input type="text" name="username" placeholder="Username">
  <input type="password" name="password" placeholder="Password">
  <button type="submit">Login</button>
</form>

GET shows data in the URL, while POST keeps it hidden.

Useful Form Attributes

AttributeDescriptionExample
actionDefines where to send the form data after submission.action="form.php"
methodSpecifies how the form data is sent โ€” usually GET or POST.method="post"
autocompleteEnables or disables browser auto-fill suggestions for input fields.autocomplete="off"
enctypeDefines how the form data should be encoded (used when uploading files).enctype="multipart/form-data"
targetSpecifies where to display the response after submitting the form (e.g., new tab, same tab, frame).target="_blank"
novalidateTemporarily disables browser form validation when submitting.novalidate

Leave a Comment