This tutorial explores how HTML forms handle data submission using the action and method attributes, along with the submit mechanism. Learn how to send form data to a server, understand the difference between GET and POST methods, and use the <button> or <input type="submit"> for submission. This beginner-friendly guide includes real-time previews and examples to make form submission clear and practical
Forms are designed to collect user input and send it to a server for processing (e.g., saving data, searching, or logging in). The action attribute defines the destination URL, while the method attribute controls how data is sent. Proper setup ensures forms work reliably and securely.
The action attribute in a <form> tag specifies the URL where form data is sent, such as a server-side script (e.g., /submit
or process.php
). If omitted, the form submits to the current page.
The method attribute defines how data is sent:
?name=John&email=john@example.com
). Suitable for non-sensitive data or search forms.This example shows a form using the GET method to submit data, with the action set to a hypothetical /search
endpoint.
<form action="/search" method="get" class="get-form">
<label for="search1">Search Query:</label><br>
<input type="text" id="search1" name="query" placeholder="Enter search term"><br>
<button type="submit">Search</button>
</form>
Preview:
This example uses the POST method to securely submit sensitive data, such as login credentials, to a /login
endpoint.
<form action="/login" method="post" class="post-form">
<label for="username1">Username:</label><br>
<input type="text" id="username1" name="username" placeholder="Enter username"><br>
<label for="password1">Password:</label><br>
<input type="password" id="password1" name="password" placeholder="Enter password"><br>
<input type="submit" value="Login">
</form>
Preview:
Both <button type="submit"> and <input type="submit"> trigger form submission. The <button> is more flexible for styling and can include HTML content (e.g., icons), while <input> is simpler but limited to text.
Create a form with both GET and POST methods to understand their behavior. Experiment with different action URLs.
<form action="/feedback" method="post" class="feedback-form">
<label for="name2">Name:</label><br>
<input type="text" id="name2" name="name" placeholder="Enter your name"><br>
<label for="message2">Feedback:</label><br>
<textarea id="message2" name="message" rows="4" cols="50" placeholder="Enter your feedback"></textarea><br>
<button type="submit">Submit Feedback</button>
</form>
Preview:
By mastering the action and method attributes, along with submit elements, you can create functional forms that send data correctly. Practice these concepts to build reliable forms!