HTML Form Attributes
This chapter describes the different attributes for the HTML <form> element.
The action Attribute
The action attribute specifies where to send the form-data when a form is submitted. This is typically a URL on a server that will process the data.
html
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<input type="submit" value="Submit">
</form>The method Attribute
The method attribute specifies the HTTP method to use when sending form-data. It can be "get" or "post".
- GET: Appends form data to the URL. Best for non-sensitive data.
- POST: Sends form data in the body of the HTTP request. More secure and required for file uploads.
html
<form action="/action_page.php" method="post">
...
</form>The target Attribute
The target attribute specifies where to display the response that is received after submitting the form.
_blank: The response is displayed in a new window or tab._self: The response is displayed in the same frame (this is the default).
html
<form action="/action_page.php" target="_blank">
...
</form>Other Common Attributes
- autocomplete: Specifies whether a form should have autocomplete on or off.
- novalidate: A boolean attribute specifying that the form-data should not be validated when submitted.
Test Yourself with an Exercise
Which attribute defines the HTTP method for submitting a form?