Categories
HTML Tutorials

The action attribute

The action attribute specifies where the browser should send the data collected from the user when the form is submitted. We want the data to be submitted to our Node.js script, which means we want the form to post to the /form URL on port 8080 of my development server, titan.

<form method="post" action="http://titan:8080/form">

If you don’t apply the action attribute to the form element, the browser will send the form data to the same URL that the HTML document was loaded from. This isn’t as useless as it might initially appear, and several popular web application development frameworks depend on this feature. If you specify a relative URL, this value is appended to the URL of the current page (or) if you used the base element —to the value of the href attribute of that element. Below we have shown how you can use the base element to set the destination for the form data.

Using the base element to set a destination for form data
<html>
  <head>
    <title>Example</title>
    <meta name="author" content="Adam Freeman"/>
    <meta name="description" content="A simple example"/>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
    <base href="http://titan:8080"/>
  </head>
  <body>
    <form method="post" action="/form">
      <input name="fave"/>
      <button>Submit Vote</button>
    </form>
  </body>
</html>

Note: The base element affects all relative URLs in an HTML document, not just the form element.