Open In App

HTML action Attribute

Last Updated : 03 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The HTML action attribute is used to specify where the form data should be sent on submission. It allows the browser to send the data to the specified location, enabling server-side scripts to process the data and generate a response.

Note: It can be used in the <form> element. 

Syntax:

<form action="URL">
HTML
<html>
<body>
	<form action="/submit-form" method="post">
		<label for="username">Username:</label>
		<input type="text" id="username" name="username" required>
		<br><br>
		<label for="email">Email:</label>
		<input type="email" id="email" name="email" required>
		<br><br>
		<input type="submit" value="Submit">
	</form>
</body>
</html>
  • The <form> element’s action attribute is set to “/submit-form”, indicating that when the form is submitted, the data will be sent to the /submit-form URL for processing.
  • The method attribute is set to “post”, specifying that the form data should be sent using the HTTP POST method.

Attribute Values: 

URL: It is used to specify the URL of the document where the data is to be sent after the submission of the form. 
The possible values of the URL are: 

URL TypeDescriptionExample
Absolute URLPoints to another website or domain.https://www.gfg.org
Relative URLRefers to a file within the same website or domain.www.geeksforgeeks.org

More Examples of HTML Action Attribute

Submitting Form Data to a Server-Side Script

HTML
<html>
<body>
	<form action="https://www.example.com/process-form" method="post">
		<label for="name">Name:</label>
		<input type="text" id="name" name="name" required>
		<br><br>
		<label for="email">Email:</label>
		<input type="email" id="email" name="email" required>
		<br><br>
		<input type="submit" value="Submit">
	</form>
</body>
</html>

In this example:

  • The action attribute is set to “https://www.example.com/process-form”, indicating that the form data will be sent to this URL for processing upon submission.
  • The method attribute is set to “post”, specifying that the form data should be sent using the HTTP POST method.

Using a Relative URL in the action Attribute

HTML
<html>
<body>
	<form action="/submit-form" method="get">
		<label for="search">Search:</label>
		<input type="text" id="search" name="query" required>
		<br><br>
		<input type="submit" value="Search">
	</form>
</body>
</html>


  • The action attribute is set to “/submit-form”, a relative URL pointing to a resource within the same domain. This means the form data will be sent to the /submit-form path on the current website.
  • The method attribute is set to “get”, indicating that the form data will be appended to the URL as query parameters.

Best Practices for HTML action Attribute

  • Specify a Valid URL: Ensure the action attribute points to a valid server endpoint that can process the submitted form data.
  • Use Appropriate HTTP Methods: Combine the action attribute with the method attribute, setting method=”post” for sensitive data submissions and method=”get” for idempotent requests like search queries.


Next Article

Similar Reads