HTML Forms: Action & Method
Master form submission with action and method attributes - the backbone of user interaction on the web.
š What You'll Learn
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.
<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
<!-- 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
<!-- 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<!-- 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<!-- 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?
| Feature | GET Method | POST Method |
|---|---|---|
| Data Visibility | Visible in URL | Hidden in body |
| Data Length | Limited (2048 chars) | Unlimited |
| Security | Less secure | More secure |
| Caching | Can be cached | Not cached |
| Bookmarking | Can be bookmarked | Cannot be bookmarked |
| Browser History | Stored in history | Not stored |
| Use Cases | Search, Filters | Login, Registration |
| Data Type | ASCII characters only | Any 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
š 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
?q=query
Hidden credentials
Email, message
User details
?category=html&sort=new
š Continue Learning
š» 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
<form action="/contact.php" method="POST">
<!-- Add your form fields here -->
</form>