HTML Forms: Action & Method

Master form submission with action and method attributes - the backbone of user interaction on the web.

Beginner Friendly20 min readInteractive DemosReal Projects

šŸ“‹ What You'll Learn

šŸŽÆForm Basics
šŸš€Action Attribute
šŸ“ØGET Method
šŸ”’POST Method
šŸ†šGET vs POST
šŸŽØForm Styling
šŸ›”ļøSecurity
⚔Best Practices

1šŸ“ Form Basics - The Building Blocks

Forms are essential for user interaction - they collect data, handle logins, process payments, and much more. Every form needs to know where to send data and how to send it.

šŸ’” Key Insight: The action and method attributes work together to determine what happens when a user submits a form.

html
<form action="/submit-data" method="POST">
  <!-- Form fields go here -->
  <input type="text" name="username" placeholder="Enter username">
  <button type="submit">Send Data</button>
</form>

šŸŽÆ Action Attribute

Specifies where to send the form data when submitted. This can be a URL, file path, or server endpoint.

šŸ“Ø Method Attribute

Defines how to send the data - either GET(visible in URL) or POST (hidden in request body).

2šŸš€ Action Attribute - Where Data Goes

šŸ“ Action Values Explained

action="/login"

Sends to /login on same domain

Most common - relative URLs

action="https://api.example.com/submit"

Sends to external server

Cross-domain submissions

action="process.php"

Sends to PHP file in same folder

Server-side processing

action="#"

Sends to current page

Self-processing forms

āœ… Correct Usage

html
<!-- Send to login endpoint -->
<form action="/api/login" method="POST">

<!-- Process contact form -->
<form action="contact.php" method="POST">

<!-- Submit to external service -->
<form action="https://forms.example.com" method="POST">

āŒ Common Mistakes

html
<!-- Missing action (stays on same page) -->
<form method="POST">

<!-- Invalid URL -->
<form action="undefined" method="POST">

<!-- Wrong file path -->
<form action="../wrong-folder/process.php" method="POST">

3šŸ“Ø Method Attribute - How Data Travels

šŸ” GET Method

Data is appended to the URL as query parameters. Perfect for search forms, filters, and any action that doesn't change server data.

Example URL after submission:

/search?query=html+forms&category=tutorials&sort=latest
html
<!-- Search Form Example -->
<form action="/search" method="GET">
  <input type="text" name="query" placeholder="Search tutorials...">
  <select name="category">
    <option value="html">HTML</option>
    <option value="css">CSS</option>
  </select>
  <button type="submit">Search</button>
</form>

šŸ”’ POST Method

Data is sent in the request body, not visible in URL. Essential for login forms, registration, payments, and any action that changes server data.

Data is hidden in request body:

username=johndoe&password=secure123&email=john@example.com
html
<!-- Registration Form Example -->
<form action="/register" method="POST">
  <input type="text" name="username" placeholder="Username" required>
  <input type="email" name="email" placeholder="Email" required>
  <input type="password" name="password" placeholder="Password" required>
  <button type="submit">Create Account</button>
</form>

4šŸ†š GET vs POST - When to Use Which?

FeatureGET MethodPOST Method
Data VisibilityVisible in URLHidden in body
Data LengthLimited (2048 chars)Unlimited
SecurityLess secureMore secure
CachingCan be cachedNot cached
BookmarkingCan be bookmarkedCannot be bookmarked
Browser HistoryStored in historyNot stored
Use CasesSearch, FiltersLogin, Registration
Data TypeASCII characters onlyAny data type

āœ… Use GET When:

  • • Retrieving data (searching, filtering)
  • • The action is safe and idempotent
  • • You want shareable URLs
  • • Data is not sensitive
  • • Small amounts of data

āœ… Use POST When:

  • • Submitting sensitive data (passwords)
  • • Uploading files
  • • Large amounts of data
  • • Changing server state
  • • Creating new resources

5šŸŽ® Interactive Form Builder

Configure Your Form

Live Preview & Code

<form action="/submit" method="GET"> <!-- Form fields --> </form>

Your Learning Progress

Forms: Action & Method75%
3/4
Topics Done
15m
Time Spent

šŸ“š Quick Reference

action="url"

Where to send form data

method="GET"

Data in URL, for searches

method=&qupt;POST"

Data in body, for security

šŸŽÆ Common Form Patterns

SearchGET

?q=query

LoginPOST

Hidden credentials

ContactPOST

Email, message

RegistrationPOST

User details

FiltersGET

?category=html&sort=new

šŸ’» Practice Exercise

Build a Contact Form

Create a contact form that sends data to /contact.php using the POST method. Include fields for name, email, and message.

Your form code will appear here

āœ… Requirements
  • • Use correct form attributes
  • • Include name, email, message fields
  • • Add a submit button
  • • Use POST method
  • • Set action to "/contact.php"
šŸ’” Solution Hint
html
<form action="/contact.php" method="POST">
  <!-- Add your form fields here -->
</form>

Master web development with WebDev Tutorials - From beginner to pro

VIDO - Learn Web Development