Forms in HTML

Forms allow users to send data to a server.

Basic Example

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

    <label for="message">Message:</label>
    <textarea id="message" name="message"></textarea>

    <button type="submit">Send</button>
</form>

Explanation

  • action: The URL where the form data will be sent.
  • method: Submission method (get or post).
  • required: Makes the field mandatory.

Best Practices

  • Use <label> tags to improve accessibility.
  • Validate data both on the client and the server.

Exercise

Create a form that asks for a name, email address, and a message.