The HTML <form>
element is used to create an interactive form on a web page. It allows users to enter and submit data to a server for processing. In addition to various input types such as text, radio buttons, checkboxes, and buttons, there are several attributes that can be used to modify the behavior of a form.
One such attribute is the action
attribute, which specifies the URL of the script or program that will process the data submitted through the form. When the form is submitted, the browser sends an HTTP request to the specified URL with the data from the form included in the request. The syntax for using the action
attribute is as follows:
php
<form action="url">
For example, if we want to send the data from our form to a PHP script called process.php
located in the same directory as our HTML file, we would use the following code:
php
<form action="process.php">
The target
attribute is used to specify where the response from the server will be displayed after the form is submitted. The default value is _self
, which means the response will replace the current page. Other possible values include _blank
, _parent
, and _top
. The syntax for using the target
attribute is as follows:
php
<form action="url" target="_blank">
For example, if we want the response to be displayed in a new window or tab, we would use the following code:
php
<form action="process.php" target="_blank">
The method
attribute is used to specify the HTTP method used to send the form data to the server. The two most common values for this attribute are GET
and POST
. The GET
method sends the form data as part of the URL query string, while the POST
method sends the data as part of the request body. The syntax for using the method
attribute is as follows:
php
<form action="url" method="post">
For example, if we want to use the POST
method to send the form data to our PHP script, we would use the following code:
php
<form action="process.php" method="post">
In conclusion, the action
, target
, and method
attributes are essential for creating functional and effective HTML forms. By specifying the appropriate values for these attributes, we can ensure that our forms behave as intended and that the data submitted by users is processed correctly.
here is a list of all the attributes that can be used with the HTML <form>
element:
accept-charset
: specifies the character encoding used for form submissionsaction
: specifies the URL to which the form data will be submittedautocomplete
: enables or disables autocomplete for form fieldsenctype
: specifies the media type used for form data submissionmethod
: specifies the HTTP method used for form data submissionname
: specifies a name for the formnovalidate
: disables browser validation for form fieldstarget
: specifies where to display the response received after form submission
It’s important to note that some of these attributes have default values, so they don’t necessarily need to be explicitly defined in the <form>
element. For example, the default value for the method
attribute is GET
, and the default value for the enctype
attribute is application/x-www-form-urlencoded
. However, it’s a good practice to define all attributes explicitly for clarity and to avoid confusion.